Fatih Erol
Fatih Erol

Reputation: 699

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

I created an empty project.

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();
        }
    }

Folder Structure

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

HomeController

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

I'm new about asp.net core, I'm just trying to understand, this url "http://localhost:41036/profile/about" always return "404 not found", it's a bit confusing. I'm doing something wrong? i will be happy if u help me.

Upvotes: 2

Views: 2301

Answers (1)

Brad
Brad

Reputation: 4553

You need a route for areas in your Startup.

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

You need to add a route attribute to your controller class.

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

Upvotes: 5

Related Questions