[英]How to configure Routes in Web.config
Im a newbie to Asp.Net Web Applications. 我是Asp.Net Web应用程序的新手。 I have created an empty ASP.Net Web Application using MVC in Visual Studio 2017. Im trying to configure Routing in the Web Application. 我已在Visual Studio 2017中使用MVC创建了一个空的ASP.Net Web应用程序。我试图在Web应用程序中配置路由。 What I have done so far is adding an "App_Start" Folder to the Project & then creating a class file called RouteConfig.cs with following Code: 到目前为止,我所做的是在项目中添加“ App_Start”文件夹,然后使用以下代码创建名为RouteConfig.cs的类文件:
RouteConfig.cs RouteConfig.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"
);
}
}
}
I have then edited the Global.asax file as below 然后,我如下编辑了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);
}
}
}
I want to now configure routing in web.config in such a way that the Url at the Browser shud appear as http://localhost:58170/Login 现在,我想在web.config中配置路由,使浏览器页面的网址显示为http:// localhost:58170 / Login
I have already build the Website & viewed it in Visual Studio IIS Express but it seems to load the Default.aspx with no Url Routing. 我已经建立了网站并在Visual Studio IIS Express中查看了它,但似乎没有URL路由就加载了Default.aspx。
the Web.Config File so far is as follows: 到目前为止,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>
How do i go about this ?? 我该怎么办?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Login",
url: "Login",
defaults: new { controller = "Login", action = "LoginIndex" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
You can use this scheme 您可以使用此方案
First route goes to http://yoururl/Login 第一条路线转到http:// yoururl / Login
Second is the default you can use as Home/Index. 第二是可以用作主目录/索引的默认值。
Home is controller name ındex is the action 家是控制器名称ındex是动作
You can use the Rewrite module. 您可以使用重写模块。 Then you can set this up like this: 然后可以这样设置:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.