从MVC Web Api中的url中删除index.html

时间:2014-02-02 23:44:28

标签: asp.net-mvc angularjs model-view-controller

我正在使用angular作为前端,我有一个index.html作为起始页面。

主页面上的

看起来像这样:

本地主机:588 / index.html的#/

我希望它看起来像:localhost:3478 /#/或者只是localhost:3478 /

问题是我的家庭控制器的索引方法我返回

    public ActionResult Index()
            {
//return File("index.html", "text/html");
            //return ActionResult("~/index.html");
            //return new RedirectResult("~/index.html", true);
                return Redirect(Url.Content("index.html"));
            }

我无法找出任何其他方法来使其发挥作用。我如何解决我的问题?

P.S。已经尝试过的代码是没有工作的。

1 个答案:

答案 0 :(得分:3)

1 - 解决方案1:

  • 将index.html移至〜/ shared文件夹并将其重命名为index.cshtml
  • 在控制器中使用以下内容:

    public ActionResult Index()
    {          
        return View();
    }
    

2 - 解决方案2:阅读index.html内容并返回HTTP响应 - 在控制器类中使用以下作为索引操作:

    public ActionResult index()
    {
        var myHtmlFile = Server.MapPath("~/Index.html");
        if (!System.IO.File.Exists(myHtmlFile))
        {
            return HttpNotFound();
        }
        return Content(System.IO.File.ReadAllText(myHtmlFile), "text/html");
    }

在这两种方式中,您可能需要更改index.html / cshtml文件中访问image,css和.js资源的路径,但这不是什么大问题,因为index.html不会出现在地址栏中。

相关问题