我目前正在使用.htaccess
将旧网站迁移到ASP.NET Core 2,我遇到了路由问题。这是一个有大约30个链接的大型网站,我必须保留旧结构的URL结构。所以我尝试扩展Startup
类的app.UseMvc(route)
函数。
这是我的代码:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes => new CustomRouting("mydomain.com",app).GetRouts());
}
}
这是我的其他课程:
public class CustomRouting
{
string domain;
IApplicationBuilder app;
public CustomRouting(string domain, IApplicationBuilder app)
{
this.domain = domain;
this.app = app;
}
public IRouteBuilder GetRouts()
{
IRouteBuilder route = new RouteBuilder(app);
switch(domain)
{
case "mydomain.com":
route = new DeRouting(app).GetRoutes();
break;
default:
route = new DeRouting(app).GetRoutes();
break;
}
return route;
}
}
public class DeRouting
{
IApplicationBuilder app;
public DeRouting(IApplicationBuilder app)
{
this.app = app;
}
public IRouteBuilder GetRoutes()
{
IRouteBuilder routes = new RouteBuilder(app);
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
//here should come more
return routes;
}
}
目前我收到以下错误:
Microsoft.AspNetCore.Routing.RouteCreationException:'默认值 必须在IRouteBuilder上设置处理程序。'
我如何为这些规则创建路由集合?将会有更多类型的URL来处理多个域。
答案 0 :(得分:0)
public class CustomRouting
{
string domain;
IApplicationBuilder app;
IRouteBuilder routeBuilder;
public CustomRouting(string domain, IApplicationBuilder app, IRouteBuilder routes)
{
this.domain = domain;
this.app = app;
this.routeBuilder = routes;
}
public IRouteBuilder GetRouts()
{
switch(domain)
{
case "mydomain.com":
route = new DeRouting(app, routeBuilder).GetRoutes();
break;
default:
route = new DeRouting(app, routeBuilder).GetRoutes();
break;
}
return route;
}
}
public class DeRouting
{
IApplicationBuilder app;
IRouteBuilder routeBuilder;
public DeRouting(IApplicationBuilder app, IRouteBuilder routes)
{
this.app = app;
this.routeBuilder = routes;
}
public IRouteBuilder GetRoutes()
{
routeBuilder.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
//here should come more
return routes;
}
}
// StartUp.cs
app.UseMvc(routes =>
{
new RouteManager(app,routes).GetRoutes();
});