asp.net-mvc 将 index.html 设置为默认页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22656945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:36:46  来源:igfitidea点击:

Set index.html as the default page

asp.net-mvcasp.net-mvc-routing

提问by vir

I have an empty ASP.NET application and I added an index.html file. I want to set the index.html as default page for the site.

我有一个空的 ASP.NET 应用程序,并添加了一个 index.html 文件。我想将 index.html 设置为站点的默认页面。

I have tried to right click on the index.html and set as start page, and when I run it the url is: http://localhost:5134/index.htmlbut what I really want is that when I type: http://localhost:5134, it should load the index.html page.

我试图右键单击 index.html 并设置为起始页,当我运行它时,url 是:http://localhost:5134/index.html但我真正想要的是,当我输入: 时http://localhost:5134,它应该加载 index.html 页面。

my route config:

我的路线配置:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

回答by vir

I added an instruction to my route config to ignore empty routes and that solved my problem.

我在路由配置中添加了一条指令以忽略空路由,这解决了我的问题。

routes.IgnoreRoute(""); 

回答by Alex Sanséau

As @vir answered, add routes.IgnoreRoute("");to RegisterRoutes(RouteCollection routes)which you should find in RouteConfig.cs by default.

作为@vir回答,添加routes.IgnoreRoute("");RegisterRoutes(RouteCollection routes)你应该在默认情况下RouteConfig.cs找到。

Here's what the method could look like:

该方法可能如下所示:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

The reason is that ASP.NET MVC takes over URL management and by default the routing is such that all extensionless URLs are controlled by the extensionless Url handler defined in web.config.

原因是 ASP.NET MVC 接管了 URL 管理,并且默认情况下路由是这样的,所有无扩展名的 URL 都由 web.config 中定义的无扩展名 URL 处理程序控制。

There's a detailed explanation here.

有一个详细的解释在这里

回答by Boris Lipschitz

Assuming the web app is running in IIS, the default page can be specified in a web.config file:

假设 Web 应用程序在 IIS 中运行,则可以在 web.config 文件中指定默认页面:

<system.webServer>
    <defaultDocument>
        <files>
            <clear />
            <add value="index.html" />
        </files>
    </defaultDocument>
</system.webServer>

回答by vickey1611

Create a new controller DefaultController. In index action, i wrote one line redirect:

创建一个新的控制器 DefaultController。在索引操作中,我写了一行重定向:

return Redirect("~/index.html")

In RouteConfig.cs, change controller="Default" for the route.

在 RouteConfig.cs 中,更改路由的 controller="Default"。

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
        );

回答by sergio

One solution is this one:

一种解决方案是这样的:

 //routes.MapRoute(
 //           name: "Default",
 //           url: "{controller}/{action}/{id}",
 //           defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
 //      );

I mean, comment or delete this code in your MVC project to avoid the default behavior when you make the initial request http://localhost:5134/.

我的意思是,在您的 MVC 项目中注释或删除此代码,以避免在您发出初始请求时出现默认行为http://localhost:5134/

The index.html must be in the root of your solution.

index.html 必须位于解决方案的根目录中。

Hope this helps! It works for me.

希望这可以帮助!这个对我有用。