How to work Action methods and URLs in ASP.Net MVC?
How to work Action methods and URLs in ASP.Net MVC?
143915-Mar-2022
Updated on 04-Jun-2023
Home / DeveloperSection / Forums / How to work Action methods and URLs in ASP.Net MVC?
How to work Action methods and URLs in ASP.Net MVC?
Aryan Kumar
04-Jun-2023Action methods and URLs are two of the most important concepts in ASP.NET MVC. Action methods are the functions that handle requests from the user, and URLs are the addresses that users use to access those functions.
In ASP.NET MVC, action methods are defined in controllers. A controller is a class that contains one or more action methods. Each action method has a name and a signature. The name of the action method is used to route the request to the correct method. The signature of the action method defines the parameters that the method accepts.
When a user requests a URL, the ASP.NET MVC framework will route the request to the controller that contains the action method that matches the URL. The action method will then be invoked and will handle the request.
Here is an example of an action method:
Code snippet
In this example, the action method is named Index and it has no parameters. The action method will handle requests to the URL /. It will then return a view named Index.
URLs in ASP.NET MVC can be defined using a variety of routing patterns. The most common routing pattern is the controller action pattern. The controller action pattern uses the name of the controller and the name of the action method to create a URL.
Here is an example of a URL that uses the controller action pattern:
Code snippet
In this example, the URL is /Home/Index. The controller name is Home and the action method name is Index.
ASP.NET MVC also supports other routing patterns, such as the resource routing pattern and the convention-based routing pattern. The resource routing pattern allows you to define URLs that represent resources, such as images, documents, and files. The convention-based routing pattern allows you to define URLs based on the names of your controllers and action methods.
Action methods and URLs are two of the most important concepts in ASP.NET MVC. By understanding how they work, you can create powerful and flexible web applications.
Kalin
15-Mar-2022ActionResult and URL in ASP.Net MVC :
When any user want to open a website or web application then user will enter a URL to find tha particular web application in the browser like given below,
http://localhost:80808/PersonDetails/Index
In the above URL you can see there are two name ‘PersonDetails’ and ‘Index’ is used after localhost and port number. Here ‘PersonDetails’ is the controller name in asp.net mvc and ‘index’ is the action method name which user wants to open using above URL.
Controller : PersonDetails
Action : Index
You can clearly understand of this process through below example.
Controller with Action method in ASP.Net MVC :
That method is called an Action method with is used with ActionResult in controller and it is return various results. In the following example there are a controller file PersonDetailsController and in that file the class is PersonDetails and index is an action method inside that class,
Exp-