加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 将ASP.NET MVC路由迁移到ASP.NET vNext

发布时间:2020-12-16 03:29:46 所属栏目:asp.Net 来源:网络整理
导读:我有一个ASP.NET MVC应用程序.我正在学习ASP.NET vNext.为此,我决定将现有的应用程序移植到vNext.我不确定的是,如何移植我的路线. 在我的原始ASP.NET MVC应用程序中,我有以下内容: RouteConfig.cs public static void RegisterRoutes(RouteCollection route
我有一个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}",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",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();
        }
    }
}

不过,我不确定两件事:

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

解决方法

免责声明:由于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: "",action = "Index" }
    );

    // instead of UrlParameter.Optional,you use the ? in the template to indicate an optional parameter
    routes.MapRoute(
      name: "Items",template: "items/{resource?}",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
}

注意:你可以在这里很好地内联路由注册,但我更喜欢将它放在单独的文件中以解决杂乱的问题.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读