ASP.NET MVC Routing to start at html page
I am using IIS 6. I think my problem is that I don't know how to route to a non controller using the routes.MapRoute.
I have a url such as example.com and I want it to serve the index.htm page and not use the MVC. how do I set that up? In IIS, I have index.htm as my start document and my global.asax has the standard "default" routing, where it calls the Home/Index.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
I added this:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Context.Request.FilePath == "/") Context.RewritePath("index.htm");
}
it works. But is this the best solution?
如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

评论(4)

routes.IgnoreRoute ?
Also, see this question: How to ignore route in asp.net forms url routing

The best solution is to remove the default Controller. You're running into this issue, because you're specifying both the default page and the default route without any parameters.
By just removing the controller = "Home"
on the route defaults, the /
won't match the route anymore and because no other route will satisfy, IIS will look into the default documents.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { action = "Index", id = "" } // Parameter defaults
);
}


I added a dummy controller to use as the default controller when the root of the web site is specified. This controller has a single index action that does a redirect to the index.htm site at the root.
public class DocumentationController : Controller
{
public ActionResult Index()
{
return Redirect( Url.Content( "~/index.htm" ) );
}
}
Note that I'm using this a the documentation of an MVC-based REST web service. If you go to the root of the site, you get the documentation of the service instead of some default web service method.
发布评论
需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。