【问题标题】:how to set start page in webconfig file in asp.net c#如何在asp.net c#的web配置文件中设置起始页
【发布时间】:2013-12-20 11:52:00
【问题描述】:

如何使用 webconfig 文件设置起始页。我已经尝试过这段代码

<system.webServer>
        <defaultDocument enabled="true">
            <files>
                <clear />
                <add value="index.aspx"/>
            </files>
        </defaultDocument>
    </system.webServer>

但它对我不起作用。我已经通过右键单击解决方案资源管理器中的页面设置起始页面,然后选择选项设置为起始页面,但我如何以编程方式进行设置

【问题讨论】:

  • 你把这个放在哪里了?你能发布完整的配置吗
  • 当您在 IIS 上运行 web 时,似乎 会起作用。如果你从 VS 运行,它会按照你设置为起始页的文件重定向。
  • 为我工作!

标签: asp.net configuration web-config


【解决方案1】:

对于那些现在使用 MVC 视图的用户,请将重写规则添加到您的 web.config:

    <rewrite>
        <rules>
            <rule name="Redirect to front" patternSyntax="ECMAScript" stopProcessing="true">
                <match url="^$" />
                <action type="Redirect" url="/yourViewName" />
            </rule>
        </rules>
    </rewrite>

只需将其放在配置标签的 system.webServer 标签之间,将 yourViewName 替换为起始页的视图名称(如 Home),就完成了!

像梦一样工作。

【讨论】:

    【解决方案2】:

    当我安装 Kaliko CMS Nuget 包时,我也遇到了同样的问题。当我删除它时,它又开始正常工作了。因此,您的问题可能是因为最近安装了 Nuget 包。卸载它,您的解决方案就可以正常工作了。

    【讨论】:

      【解决方案3】:

      如果您的项目包含 RouteConfig.cs 文件,那么您可能需要通过在此文件中添加 routes.IgnoreRoute(""); 来忽略到根目录的路由。

      如果它不能解决你的问题,试试这个:

      void Application_BeginRequest(object sender, EventArgs e)
      {
          if (Request.AppRelativeCurrentExecutionFilePath == "~/")
              Response.Redirect("~/index.aspx");
      }
      

      【讨论】:

      • IgnoreRoute 帮助
      【解决方案4】:
      I think this will help
      

          <directoryBrowse enabled="false" />
          <defaultDocument>
            <files>
              <clear />
              <add value="index.aspx" />
              <add value="Default.htm" />
              <add value="Default.asp" />
              <add value="index.htm" />
              <add value="index.html" />
              <add value="iisstart.htm" />
              <add value="default.aspx" />
              <add value="index.php" />
            </files>
          </defaultDocument>
        </system.webServer>
      

      【讨论】:

      • 为什么我们需要添加所有这些?订单是否添加了用于显示它们的标准?
      • 如果您有 .aspx 页面,则仅添加这些页面,否则它可以是任何页面,具体取决于您使用的语言。
      【解决方案5】:

      以下代码对我来说很好用。请检查您的网络配置中的其他设置

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

      【讨论】:

      • @Aravin 这意味着它会清除可能在影响您网站的其他 .config 中的 files 标签中定义的所有信息。
      【解决方案6】:

      您也可以通过代码实现它,在 Session_Start 事件中的 Global.asax 文件中,将 response.redirect 写入您的起始页,如下所示。

      void Session_Start(object sender, EventArgs e)
              {
                  // Code that runs when a new session is started
      
                  Response.Redirect("~/Index.aspx");
      
              }
      

      您可以从数据库或任何其他存储中获取重定向页面名称,以在应用程序运行时更改应用程序起始页,无需编辑 web.config 或更改任何 IIS 设置

      【讨论】:

      • Session_Start 每次都会被调用,并且你强制用户在任何回发时被重定向
      猜你喜欢
      • 2017-03-31
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 2012-08-22
      • 1970-01-01
      • 2011-03-12
      相关资源
      最近更新 更多