[转]How to use an Area in ASP.NET Core
2017-05-04 09:48
901 查看
本文转自:http://stackoverflow.com/questions/36535511/how-to-use-an-area-in-asp-net-core
Q:
How does one use an
Supposing I have an app that needs an Admin section. This section requires its Views to be places in that area. All requests that start with
A:
In order to include an Area in an ASP.NET Core app, first we need to include a conventional route in the
In Startup.cs/Configure method:
Then make a folder named
Now we create a controller inside the
Now in order for that to work, you'll need to create Views for all actions that return one. The hierarchy for views is just like what you have in a non-area Views folder:
Now, you should be good to go!
Question: What if I what to have another controller inside my Area?
Answer:
Just add another controller beside
The important part is
Q:
How does one use an
Areain ASP.NET Core? This is not properly documented!
Supposing I have an app that needs an Admin section. This section requires its Views to be places in that area. All requests that start with
Admin/will need to be redirected to that area.
A:
In order to include an Area in an ASP.NET Core app, first we need to include a conventional route in the
Startup.csfile (It's best to place it before any non-area route):
In Startup.cs/Configure method:
app.UseMvc(routes => { routes.MapRoute("areaRoute", "{area:exists}/{controller=Admin}/{action=Index}/{id?}"); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });
Then make a folder named
Areasin the app root and make another named
Admininside the former, also make these folders inside
Admin(ViewComponent is optional):
Now we create a controller inside the
Controllersfolder named
AdminController, the content can be like:
[Area("Admin")] [Route("admin")] public class AdminController : Controller { public AdminController() { // do stuff } public IActionResult Index() { return View(); } [Route("[action]/{page:int?}")] public IActionResult Orders() { return View(); } [Route("[action]")] public IActionResult Shop() { return View(); } [Route("[action]/newest")] public IActionResult Payments() { return View(); } }
Now in order for that to work, you'll need to create Views for all actions that return one. The hierarchy for views is just like what you have in a non-area Views folder:
Now, you should be good to go!
Question: What if I what to have another controller inside my Area?
Answer:
Just add another controller beside
AdminControllerand make sure the routes are like the following:
[Area("Admin")] [Route("admin/[controller]")] public class ProductsController : Controller { public ProductsController() { // } [Route("{page:int?}")] public IActionResult Index() { return View(); } }
The important part is
[Route("admin/[controller]")]. With that you can keep the style of routing to
admin/controller/action/...
相关文章推荐
- How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core
- How to use HTML-5 data-* attributes in ASP.NET MVC
- How to use Oracle 11g ODP.NET UDT in an Oracle Stored Procedure's Where clause
- How to get URL and QueryString value in an ASP.NET page
- How to use Console.WriteLine in ASP.Net (C#) during debug?
- [转]How to override HandleUnauthorizedRequest in ASP.NET Core
- how to use Form Authentication in ASP.NET.
- How to Confirm a Delete in an ASP.NET Datagrid...
- How to set an IIS Application or AppPool to use ASP.NET 3.5 rather than 2.0
- How to use Console.WriteLine in ASP.Net (C#) during debug?
- [转]An introduction to OAuth 2.0 using Facebook in ASP.NET Core
- How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET(利用Ado.net导出到Excel)
- How to use FtpWebRequest in asp.net
- How to use jquery or ajax to update razor partial view in c#/asp.net for a MVC project
- how to use javascript to control the usercotrol in the asp.net
- How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET
- How to use an ASP.NET application to query an Indexing Service catalog by using Visual Basic .NET
- How to use ValidationSummary control in asp.net
- How To Use Cookies in an ASP Page
- How to use AspnetUpload™ in your web application