首页 > 解决方案 > 如何在 Web.config 中配置路由

问题描述

我是 Asp.Net Web 应用程序的新手。我在 Visual Studio 2017 中使用 MVC 创建了一个空的 ASP.Net Web 应用程序。我试图在 Web 应用程序中配置路由。到目前为止,我所做的是将“App_Start”文件夹添加到项目中,然后使用以下代码创建一个名为 RouteConfig.cs 的类文件:

路由配置.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;

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

            routes.MapPageRoute(
                routeName: "Login",
                routeUrl: "Login",
                physicalFile: "~/Default.aspx"
                );
        }

    }
}

然后我编辑了 Global.asax 文件,如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;

namespace WebApplicationBS_Web.App_Start
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

我现在想在 web.config 中配置路由,使浏览器 shud 上的 Url 显示为http://localhost:58170/Login

我已经建立了网站并在 Visual Studio IIS Express 中查看了它,但它似乎加载了没有 URL 路由的 Default.aspx。

到目前为止的 Web.Config 文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.7"/>
    <httpRuntime targetFramework="4.7"/>
  </system.web>
  <location>
    <system.webServer>
      <defaultDocument>
        <files>
          <clear/>
          <add value="Default.aspx"/>
        </files>
      </defaultDocument>
    </system.webServer>
  </location>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

我该怎么办?

标签: c#asp.net

解决方案


您可以使用重写模块。然后你可以这样设置:

<configuration>
  <system.webServer>
    <rewrite>
      <rule name="login" patternSyntax="Wildcard">
        <match url="*" />
        <conditions>
            <add input="{PATH_INFO}" pattern="/Login*" />
        </conditions>
        <action type="Redirect" url="/default.aspx" />
      </rule>
    </rewrite>
  </system.webServer>
<configuration>

推荐阅读