为什么不起作用 ASP.NET Core, Attribute Routing in Areas

Why does not work ASP.NET Core, Attribute Routing in Areas

我创建了一个空项目。

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
|       

家庭控制器

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

我是 asp.net 核心的新手,我只是想了解,这个 url "http://localhost:41036/profile/about" 总是 return " 404 未找到”,这有点令人困惑。我做错了什么?如果你能帮助我,我会很高兴。

您需要一条通往 Startup 中区域的路线。

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

您需要为您的控制器添加路由属性class。

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