繁体   English   中英

如何在Web.config中配置路由

[英]How to configure Routes in Web.config

我是Asp.Net Web应用程序的新手。 我已在Visual Studio 2017中使用MVC创建了一个空的ASP.Net Web应用程序。我试图在Web应用程序中配置路由。 到目前为止,我所做的是在项目中添加“ App_Start”文件夹,然后使用以下代码创建名为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"
                );
        }

    }
}

然后,我如下编辑了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中配置路由,使浏览器页面的网址显示为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>

我该怎么办?

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 }
        );
    }

您可以使用此方案

第一条路线转到http:// yoururl / Login

第二是可以用作主目录/索引的默认值。

家是控制器名称ındex是动作

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

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM