JQu*_*ile 5 asp.net-mvc asp.net-core
我有一个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" }
);
}
Run Code Online (Sandbox Code Playgroud)
现在在这个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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
不过,我不确定两件事:
views/home/Index.cshtml我的默认路径代替app.UseWelcomePage().注册路线:
变化不大,但总体方法保持不变。这是你的重构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).
}
}
Run Code Online (Sandbox Code Playgroud)
启动.cs
public void Configure(IApplicationBuilder app)
{
// ... other startup code
app.UseMvc(RouteConfig.RegisterRoutes);
// ... other startup code
}
Run Code Online (Sandbox Code Playgroud)
注意:您可以在这里很好地内联路由注册,但我更喜欢将其放在单独的文件中以消除混乱Startup.cs
要指出UseWelcomePage您自己的重载之一,请查看此处的不同重载。
免责声明:由于 vNext 仍处于测试阶段,每小时都会发生变化,因此我在此显示的代码可能很快就会过时,即使是在下一分钟或下一小时!
| 归档时间: |
|
| 查看次数: |
3208 次 |
| 最近记录: |