gpt4 book ai didi

c# - 为什么 ASP.NET Core,区域中的属性路由不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:55 24 4
gpt4 key购买 nike

我创建了一个空项目。

Startup.cs

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix).AddDataAnnotationsLocalization();

services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("de-DE"),
new CultureInfo("tr-TR"),
};

options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
// options.RequestCultureProviders = new List<IRequestCultureProvider> { new CookieRequestCultureProvider() };
});

services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{

var localizationOption = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();

app.UseRequestLocalization(localizationOption.Value);

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseMvc();
}
}

文件夹结构

Project
|
|--Areas
|---Profile
|----Controllers
| HomeController
|

家庭 Controller

[Area("Profile")]
public class HomeController : Controller
{
[Route("About")]
public IActionResult About()
{
return View();
}
}

我是 asp.net core 的新手,我只是想了解,这个 url“http://localhost:41036/profile/about”总是返回“404 not found”,这有点令人困惑。我做错了什么?如果你能帮助我,我会很高兴。

最佳答案

您需要为您的Startup 中的区域提供路线。

app.UseMvc(routes =>
{
routes.MapRoute("areas", "{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

您需要向您的 Controller 类添加路由属性。

[Area("Profile")]
[Route("[area]/[controller]")]
public class HomeController : Controller
{
}

关于c# - 为什么 ASP.NET Core,区域中的属性路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49439064/

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