【发布时间】:2020-03-26 15:11:21
【问题描述】:
感觉我在这里做了一些愚蠢的事情,我在玩 ASP.NET Core 3 (MVC),做一些教程,熟悉一些 - 我在路由方面遇到了一些问题。
我的Startup.cs 中有以下代码,试图设置Main/Home/{team} 的路线。
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews(mvc => mvc.EnableEndpointRouting = true)
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver = new DefaultContractResolver()
)
.AddRazorRuntimeCompilation();
services.AddKendo();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute(
name: "team",
pattern: "Main/Home/{team}");
//endpoints.MapControllerRoute(
// name: "default",
// pattern: "{controller=Main}/{action=Home}/{id?}");
});
}
在我的Main 控制器上。动作Home 有一个参数team
public class MainController : Controller
{
private readonly ILogger<MainController> _logger;
public MainController(ILogger<MainController> logger)
{
_logger = logger;
}
public IActionResult Home(string team)
{
TeamModel model = new TeamModel(team);
return View(model);
}
}
无论我做什么,我都无法将team 参数作为路由值正确通过。
下面的配置每次都会给我一个404,无论是URL(/Main/Home/MyTeam 还是/Main/Home?team=MyTeam)。
其他情况要么给我上述问题,要么team 参数带有null 值..
任何帮助都会很棒 - 我认为可能正在做一些愚蠢的事情!
【问题讨论】:
-
为什么不用默认路由,把“team”参数名改成id?
-
因为我正在学习教程和适应 - 我想以正确的方式去做。看起来应该是一个简单的答案。
标签: c# asp.net-core asp.net-core-mvc asp.net-mvc-routing