gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:54:24 28 4
gpt4 key购买 nike

我有一个 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()

最佳答案

注册路线:有一些变化,但总体方法保持不变。这是您重构的 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 .

免责声明:由于 vNext 仍处于测试阶段,每小时都会发生变动,因此我在此处显示的代码可能很快就会过时,即使是在下一分钟或下一小时!

关于asp.net-mvc - 将 ASP.NET MVC 路由迁移到 ASP.NET vNext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27799533/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com