【问题标题】:Migrating ASP.NET MVC Routes to ASP.NET vNext将 ASP.NET MVC 路由迁移到 ASP.NET vNext
【发布时间】:2015-03-04 04:07:06
【问题描述】:

我有一个 ASP.NET MVC 应用程序。我正在学习 ASP.NET vNext。为此,我决定将我现有的应用程序移植到 vNext。我不确定的是,如何移植我的路线。

在我的原始 ASP.NET MVC 应用程序中,我有以下内容:

RouteConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.RouteExistingFiles = true;

  routes.MapRoute(
    name: "Index",
    url: "",
    defaults: new { controller = "Root", action = "Index" }
  );

  routes.MapRoute(
    name: "Items",
    url: "items/{resource}",
    defaults: new { controller = "Root", action = "Items", resource = UrlParameter.Optional }
  );

  routes.MapRoute(
    name: "BitcoinIntegration",
    url: "items/available/today/{location}",
    defaults: new { controller="Root", action="Availability", location=UrlParameter.Optional }
  );

  routes.MapRoute(
    name: "BlogPost1",
    url: "about/blog/the-title",
    defaults: new { controller = "Root", action = "BlogPost1" }
  );
}

现在在这个 ASP.NET vNext 世界中,我不确定如何设置路由。我有以下内容:

Startup.cs

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;

namespace MyProject.Web
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseErrorPage();

            app.UseServices(services =>
            {
                services.AddMvc();
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
            });

            app.UseMvc();
            app.UseWelcomePage();
        }
    }
}

不过,我不确定两件事:

  1. 如何添加我之前在 RouteConfig.cs 中定义的路由。
  2. 如何使用views/home/Index.cshtml 代替app.UseWelcomePage() 作为我的默认路径。

【问题讨论】:

  • app.UseMvc(routes => RouteConfig.RegisterRoutes(routes)) 有什么问题?这不是 vNext 的区别,它只是常规的 MVC,除了您使用 OWIN 引导您的应用程序而不是在 Global.asax 中显式注册您的路线。路由的工作原理相同,您只是从不同的地方调用RegisterRoutes。至于您的其他 Home/Index 问题,只需删除对 UseWelcomePage 的调用 - 您的路由将执行它本来会执行的操作。
  • @AntP - vNext 中是否有更“推荐”的方法?我试图尽可能地与 vNext 保持一致,以便我以正确的方式学习它。谢谢。
  • 您仍然需要在传递给UseMvc 的委托中包含您的路由 - 无论您是否在其中调用RouteConfig.RegisterRoutes(routes) 或只是声明一个匿名委托并在那里添加所有路由代码(就像在您当前的示例中一样)实际上几乎没有什么区别。我可能会将其保留在 RouteConfig 中,但只是为了避免使 Configure 方法膨胀。 vNext 约定只是规定您使用 OWIN 引导您的应用程序(而不是使用 Global.asax 方法),您已经在这样做了。

标签: asp.net-mvc asp.net-core


【解决方案1】:

免责声明:由于 vNext 仍处于测试阶段,每小时都会发生变化,我在这里展示的代码可能很快就会过时,即使在下一分钟或一小时内!如果发生这种情况,在您对“这不起作用”投反对票之前,请给我留言,我会尽力将其提升到当前水平。

注册路线: 变化不大,但总体方法保持不变。这是你重构的RouteConfig

RouteConfig.cs

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Routing;

public class RouteConfig 
{
  // instead of RouteCollection, use IRouteBuilder
  public static void RegisterRoutes(IRouteBuilder routes)
  {
    // routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); -> gone
    // routes.RouteExistingFiles = true; -> gone

    routes.MapRoute(
      name: "Index", 
      template: "", 
      defaults: new { controller = "Root", action = "Index" }
    );

    // instead of UrlParameter.Optional, you use the ? in the template to indicate an optional parameter
    routes.MapRoute(
      name: "Items", 
      template: "items/{resource?}", 
      defaults: new { controller = "Root", action = "Items" }
    );

    ... // ignored for brevity (since their registration is along the same lines as the above two).
  }
}

Startup.cs

public void Configure(IApplicationBuilder app)
{
  // ... other startup code

  app.UseMvc(RouteConfig.RegisterRoutes);  

  // ... other startup code
}

注意:您可以在这里很好地内联路由注册,但我更喜欢将其放在单独的文件中以消除混乱Startup.cs

要将UseWelcomePage 指向您自己的一个,请查看不同的重载here

【讨论】:

  • 是否需要将RouteConfig注册为临时服务并注入到Configure方法中?
  • 我认为这太过分了。
猜你喜欢
  • 2011-10-16
  • 1970-01-01
  • 2023-04-10
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 2021-11-21
相关资源
最近更新 更多