No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
AspNetCoreAreaSample
.gitattributes
.gitignore
AspNetCoreAreaSample.sln
README.md

README.md

AspNetCoreAreaSample

エリア属性付きコントローラ

....
namespace AspNetCoreAreaSample.Area1.Controllers
{
    [Area("area1")]
    public class HomeController : Controller
    {
        ....
    }
}

ファイルツリー構造

Project/
    Controllers/
        HomeController.cs
    Views/
        Home/
            Index.cshtml
            About.cshtml
    Areas/
        area1/
            Controllers/
                HomeController.cs
            Views/
                Home/
                    Index.cshtml
                    About.cshtml
        area2
            Controllers/
                HomeController.cs
            Views/
                Home/
                    Index.cshtml
                    About.cshtml

エリア付きルート設定

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
    // ★ここを追加
    routes.MapRoute(
        name: "area-route",
        template: "{area}/{controller=Home}/{action=Index}/{id?}");
});

ドメインによるエリアルート設定

app.MapWhen(
    (HttpContext context) =>
    {
        return context.Request.Host.Value.StartsWith("area1.");
    },
    (IApplicationBuilder builder) =>
    {
        builder.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "area1route",
                template: "{controller=Home}/{action=Index}/{id?}",
                defaults: new { area = "area1" }
            );
        });
    }
);
app.MapWhen(
    (HttpContext context) =>
    {
        return context.Request.Host.Value.StartsWith("area2.");
    },
    (IApplicationBuilder builder) =>
    {
        builder.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "area2route",
                template: "{controller=Home}/{action=Index}/{id?}",
                defaults: new { area = "area2" }
            );
        });
    }
);