【问题标题】:How to configure Routes in Web.config如何在 Web.config 中配置路由
【发布时间】:2018-04-25 13:45:18
【问题描述】:

我是 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 中配置路由,使浏览器 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>

我该怎么办??

【问题讨论】:

  • 我想知道您是否有向后路由的想法。听起来您希望在转到默认站点时 URL 显示为 localhost:58170/Login?但是您的路由只是意味着当您转到该 URL 时,将显示默认站点。最重要的是,了解您要访问的 URL 以及您希望看到的内容会很有帮助。

标签: c# asp.net


【解决方案1】:

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

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

【讨论】:

  • 我已经用上面的代码重新配置了web.config,但它似乎给出了一个错误“HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory. A default document is not configured对于请求的 URL,服务器上未启用目录浏览。"
【解决方案2】:
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

Second 是您可以用作 Home/Index 的默认值。

Home 是控制器名称 ıindex 是动作

【讨论】:

  • 我在帖子中进行了编辑,该项目用于 MVC,但它是一个空项目。
  • 好的。所以我需要创建一个名为 Login 的文件夹,然后添加 Index.aspx
  • 首先您应该创建一个名为 controllers 的文件夹。然后在里面添加一个控制器
  • 您应该首先创建控制器文件夹和视图文件夹,以便您可以添加控制器类。你可以在这里找到详细信息docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/…
  • ok.. 但是 mvc 只成功加载 .cshtml 文件而不是 login.aspx 文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-14
  • 1970-01-01
相关资源
最近更新 更多