In this tutorial I will talk about what attribute routes are as well as their uses I will also briefly go through Action Results and give examples of this.
Attribute routes
Firstly, routing is the process of determining a url that maps a browser request to a specific MVC controller action. MVC 5 has support for another type of routing named “Attribute Routing”, a reason as to why this is useful is that it enables the functionality of defining routes above a controllers action method using something called “attributes”. Another purpose for this may be that you want a descriptive action method name so the developer knows what the method does, however, you also want an easy to remember url route for the user to remember. In addition, with attribute routing it is easier to define routes which have a hierarchy of resources such as /product/item/id
I am using Rider and will create an ASP.NET Core MVC project, you can use Rider too or maybe Visual Studio/Visual Studio Code.
So after the project has finished generating navigate to the “Controllers” folder and add another “Controller” (class file) and call it “HelloWorldController” and inherit the base class “Controller”. Also add a default action method named “Index” which returns a string.
public class HelloWorldController:Controller
{
public string Index()
{
return "Index...";
}
}