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中定义的路由. 解决方法
免责声明:由于vNext仍处于测试阶段,每小时都在进行一次流失,我在这里展示的代码很快就会过时,即使在下一分钟或下一个小时!如果发生这种情况,在您投票支持“这不起作用”之前,请给我发表评论,我会尽力将其提升到当前水平.
注册路线: 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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 控制权限的最佳做法?
- ASP.NET Web API中除IQueryable之外的OData查询和类型
- asp.net-mvc – 如何在.NET MVC中防止多个表单提交而不使用
- asp.net-mvc-4 – 在MVC4环境中使用Razor和Angular.js
- ASP.Net repeater item.DataItem为null
- asp.net – 当我在gridview中进行排序时触发了Gridview Row
- 深蓝词库转换1.7发布,支持QQ分类词库解析
- ASP.NET Core RC2无法找到Html编码器实现
- 好书推荐
- asp.net – ReportViewer控件返回空白页面