问题
For much of my web site, I want normal routing to occur the MVC way. However, when the app first launches, I don't want the route to go to /Home/Index.cshtml. I want it to go to simply /Index.html
My current RegisterRoutes looks like this (and does not achieve my goal)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("index.html");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
回答1:
public ActionResult Index() {
string FilePath = Server.MapPath("~/index.html");
// You can add other conditions also here
if (System.IO.File.Exists(FilePath)) {
return File(FilePath, "text/html");
}
return View();
}
Hope this helps!
回答2:
i don't know if you take this into account, in the root web.config you should set:
<appSettings>
<add key="webpages:Enabled" value="true" />
</appSettings>
you don't need anything else in the RouteConfig.cs if you have a index.cshtml in the root (this file can contain only html code ofcourse).
But if you want to serve (not process) the file only, i mean, index.html for example you need also to set up this file as start Page in the project and thats all, simpliest answer.
回答3:
Define the routes for all your controllers and remove the default root:
name: "Default",
url: "{controller}/{action}/{id}",
The new default will be index.html.
来源:https://stackoverflow.com/questions/14406664/with-asp-net-mvc4-how-can-i-make-my-root-index-html-be-executed-by-default