【问题标题】:How to set default page asp.net [duplicate]如何设置默认页面asp.net [重复]
【发布时间】:2013-08-02 21:20:02
【问题描述】:

我刚刚在我的服务器上发布了我的网站,但是当我在浏览器中输入 www.mysite.com 时,我收到了这个错误:HTTP Error 403.14 - Forbidden Web 服务器配置为不列出此目录的内容。但是,如果我键入 www.mysite.com/Home.aspx,它会正确加载。那么,如何设置默认页面?我的 web.config 中已经有了这个:

<system.webServer>
   <defaultDocument>
     <files>
       <add value="Pages/Home.aspx" />
     </files>
   </defaultDocument>
  </system.webServer>

【问题讨论】:

    标签: asp.net .net asp.net-mvc asp.net-core .net-core


    【解决方案1】:

    ASP.NET 网络表单

    web.config 文件上,尝试这个在之前使用clear 标签:

    <system.webServer>
      <defaultDocument>
        <files>
          <clear />
          <add value="Pages/Home.aspx" />
        </files>
      </defaultDocument>
    </system.webServer>
    

    看这里:http://www.iis.net/configreference/system.webserver/defaultdocument

    ASP.NET MVC / ASP.NET CORE

    根据您使用的 asp.net mvc 版本,您可以将它放在不同的文件中(~/Global.asax.cs 在 v3 或更早版本或~/App_Start/RouteConfig.cs 在 v4 或更高版本)。在这两种情况下,您都会看到注册路由的内容,因为 asp.net mvc 使用路由而不是像 webforms 这样的文件。因此,您可以更改默认值:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new 
            { 
                controller = "Home", // default controller
                action = "Index",  // default action on the controller
                id = UrlParameter.Optional
            }
        );
    }
    

    ASP.NET CORE 上类似。

    看这里:http://www.codeproject.com/Articles/624181/Routing-Basics-in-ASP-NET-MVC

    【讨论】:

    • 好吧,如果我要加载的默认页面放在文件夹中(在本例中为“Pages”),我是否必须在默认文档中添加“Pages/Home.aspx”???
    • 这是否适用于不在根目录中的文档?例如,如果我希望我的默认索引为“mydomain.com/folder1/index.html”
    • @Felipe,它对我不起作用,我在 ASP.net Web 窗体中使用 IIS Express
    • 你写了“在 ASP.NET Core 上是类似的”但是没有“id = UrlParameter.Optional”,所以这是一个错误。
    • 如果您使用 & 符号,请确保您使用 &而不是 &
    【解决方案2】:

    除了 Felipe 的回答,您还可以从 IIS 执行此操作。

    选择Admin Tools --> IIS Manager --> 从列表中选择您的网站。单击右侧的Default Document,然后单击Add。使用箭头将条目移动到列表顶部。你已经完成了。

    但是,每次您发布网站时,这都会被覆盖。

    【讨论】:

    • 为其他疲倦的旅行者添加 - 下次发布网站时会覆盖此设置。
    猜你喜欢
    • 2010-12-27
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 2015-11-27
    • 2012-04-19
    相关资源
    最近更新 更多