c# - 如何在 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=\"Web\" /optionInfer+"/>
</compilers>
</system.codedom>
</configuration>
我该怎么办?
解决方案
您可以使用重写模块。然后你可以这样设置:
<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>
推荐阅读
- java - Java NIO Socketchannel 只在关闭输出后发送数据。为什么是这样?
- terraform - 在 Terraform 中管理模块版本
- python - 在 Python Cloud Function 中使用错误处理程序返回 JSON
- r - 在 Windows 10 (x64) 下无法读取 R 中的 microsoft access 数据库
- excel - 使用 .bat 文件和任务调度程序执行 .vbs 文件
- sql - 已经有一个名为##ObjectName 的对象
- python - 当我们处理大长度的数字时如何减少python中的时间限制
- scala - S3AbortableInputStream:并非所有字节都从 S3ObjectInputStream 中读取
- r - 使用两个列表中的公共信息创建矩阵
- python - 根据条件替换熊猫数据框列中的数据,如果条件不满足则跳过