.Net: Does controller have to have file name corresponding to URL?

643 Views Asked by At

I'm following this tutorial on .Net controllers, and it says "imagine that you enter the following URL into the address bar of your browser: http://localhost/Product/Index/3. In this case, a controller named ProductController is invoked."

What I Want To Know:

In order to successfully hit http://localhost/Product/Index/3, do you need a controller called ProductController specifically?

2

There are 2 best solutions below

0
S.P Sarkar On

No, it is not necessary. You can use Route Attribute.

[Route("new-name-for-product")]
public class ProductController{

}

now you have to use http://localhost/new-name-for-product/Index/ this URL to invoke ProductController. If you want to use one or more parameters with this URL you have to use different route templates for ActionMethod. Example below.

[Route("new-name-for-product")]
public class ProductController
{

// http://localhost/new-name-for-product/3/ will show product details based on id
// http://localhost/new-name-for-product/Index/3/ will show product details based on id

[HttpGet]
[Route("/{id}")]
public IActionResult Index(int id)
{
// your code
}

// you can use a different action method name. 
// http://localhost/details/3/ will show product details based on id
// but parameter name (Ex: id) and the id inside route template the spelling must be the same.

[HttpGet]
[Route("details/{id}")]
public IActionResult GetById(int id)
{
// your code
}
}
0
Bob Ash On

It depends. In ASP.Net Core, routing can be configured either as conventional routing or as attribute routing.

Conventional routing is configured as below:

routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); 

Here, the first path segment maps to the controller name, the second maps to the action name, the third segment is used for an optional id used to map to a model entity.

As a convention, controller file name is usually same as controller class name. Hence, In conventional routing, url will match with filename.

The URL http://localhost/Products/Index matches below action method in ProductsController.

[Route("[controller]")]
public class ProductsController : Controller
{
   [HttpPost("Index")]     // Matches 'Products/Index'  
   public IActionResult Index()
    {
        return View();
    }
}

Attribute Routing

With attribute routing the controller name and action names play no role in which action is selected. Hence, it is independent of file name.

The URL http://localhost/Items/All matches below action method in ProductsController.

public class ProductsController : Controller
{
   [Route("Items/All")]
   public IActionResult Index()
   {
      return View();
   }
}

Similarly, [Route] attribute can be added at both Controller and action methods. The same URL http://localhost/Items/All matches the action method shown below:

[Route("Items")]
public class ProductsController : Controller
    {
       [Route("All")]
       public IActionResult Index()
       {
          return View();
       }
    }

For more details, you can refer to microsoft docs at https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1