Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net core 2 Prefix Routing

How to create prefixed routing for MVC CRUD operation. I am working on an application that requires admin and front-end. For the admin I want all route to point to localhost:5000/admin/....

I have different Controllers

public class RoomsController : Controller
{
    // GET: Rooms        
    public async Task<IActionResult> Index()
    {

        return View(await _context.Rooms.ToListAsync());
    }

    //...
}

and

public class SlidersController : Controller
{
    private readonly ApplicationDbContext _context;

    public SlidersController(ApplicationDbContext context)
    {
        _context = context;
    }

    // GET: Sliders
    public async Task<IActionResult> Index()
    {
        return View(await _context.Sliders.ToListAsync());
    }

    //...
}

Now I want the admin route to be

localhost:5000/admin/rooms
localhost:5000/admin/slider

while other routes remain

localhost:5000/
localhost:5000/about
localhost:5000/...
like image 964
rilly009 Avatar asked Dec 21 '17 11:12

rilly009


People also ask

What is difference between route and RoutePrefix?

Annotates a controller with a route prefix that applies to all actions within the controller. As you can see, the description for Route mentions exposing the action(s), but RoutePrefix does not.

How do you override the prefix route?

To override the RoutePrefix we need to use the ~ (tilde) symbol as shown below. With the above change, now the GetTeachers() action method is mapped to URI “/tech/teachers” as expected.

What is use of UseRouting and UseEndpoints in startup configure method?

UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline. It runs the delegate associated with the selected endpoint.

How do I use MapControllerRoute?

MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); The route names give the route a logical name. The named route can be used for URL generation. Using a named route simplifies URL creation when the ordering of routes could make URL generation complicated.


2 Answers

You can also use Attribute Routing for this. Till ASP.Net Web API we have the attribute named [RoutePrefix], but in ASP.Net Core 2 we can use [Route] attribute for the same purpose.

[Route("api/[controller]/[action]")]
public class DistrictController : ControllerBase
{

    [Route("{id:int:min(1)}")] // i.e. GET /api/District/GetDetails/10
    public IActionResult GetDetails(int id)
    {
    }

    // i.e. GET /api/District/GetPage/?id=10
    public IActionResult GetPage(int page)
    {
    }

    [HttpDelete]
    [Route("{id:int:min(1)}")] // i.e. Delete /api/District/Delete/10
    public IActionResult Delete(int id)
    {
    }

    [HttpGet]
    [Route("~/api/States/GetAllState")] // i.e. GET /api/States/GetAllState
    public IActionResult GetStates()
    {
    }
}
like image 62
Nilay Avatar answered Oct 06 '22 08:10

Nilay


I solve the Problem by using MVC Area docs

like image 28
rilly009 Avatar answered Oct 06 '22 06:10

rilly009