ASP.NET MVC Routing to start at html page

ASP.NET MVC Routing to start at html page

睫毛上残留的泪 发布于 2021-11-28 字数 1104 浏览 838 回复 4 原文

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

愿与i 2022-06-07 4 楼

routes.IgnoreRoute ?

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

洛阳烟雨空心柳 2022-06-07 3 楼

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
    );
}
幸福%小乖 2022-06-07 2 楼

Configure the asp.net routing to ignore root ("/") requests and let IIS's "Default Document" ISAPI filter serve the static index.htm file

Add the following to the RegisterRoutes method.

routes.IgnoreRoute("");
秉烛思 2022-06-07 1 楼

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.