ASP.NET 5 is an open rise web application outgrowth framework for edifice present web applications. It is built on the .NET Core runtime and you can take gain of it to build and run applications on Windows_ Linux_ and the Mac. ASP.NET Core MVC is a lightweight_ open rise_ greatly testable framework built on top of the ASP.NET Core runtime and is useful as part of ASP.NET 5.
When working in ASP.NET Core MVC_ you might frequently need to feel actions that are not known at outgrowth time. This article talks almost how you can feel mysterious action orders in ASP.NET Core 5. The code samples are given in C#.
open class AuthorController : Controller{ [HttpGet] open ActionResult Index { recur View; } }<_pre> In ASP.NET Core MVC_ a controller is a class that is typically named with the suffix “Controller” and is used to mark and logically cluster a assembly of correspondent actions. Whenever you form a new controller_ a lapse action order named Index is formd automatically.
<_aside> The lapse action order in ASP.NET Core MVC
Note that the name of the lapse action order_ Index_ is specified in the Conaspect order of the Startup class as shown in the code snippet given under.
app.UseEndpointsendpoints _gt;{ endpoints.MapControllerRoute name: "lapse"_ model: "{controller_Home}_{action_Index}_{id?}"; };<_pre> Because the action name has been specified as Index in the precedent code snippet_ you must have a file named Index.cshtml in the Views_Author folder of your project. You can now run the application and browse whichever of the pursueing endpoints:
http:__localhost:51913_Author
http:__localhost:51913_Author_Index Youll remark that the Index page will be displayed in both cases.
<_aside> The problem with mysterious action orders
Lets now apprehend the problem well explain in this sample. When working with ASP.NET Core MVC you will frequently need action orders that are not known at the time of outgrowth of the application.
Suppose you are edifice a searchable directory of openations_ and you want users to be able to search by creator name_ or by book or article title. You would need to know the details of all the creators_ books_ and articles the directory will hold i.e._ creator names_ book titles_ article titles_ openation dates_ and so on in advance. How do you execute that?
To execute this_ you would need to have the corresponding views i.e._ Authors.cshtml_ Books.cshtml_ Articles.cshtml_ etc. in the Views folder as well as action orders that can be invoked using URLs such as the pursueing:
_Author_Books
_Author_Articles Consider the pursueing URL_ which easily maps to the corresponding action order_ namely the Index order of the AuthorController.
_Author_Index
However_ if you try to browse whichever of the pursueing endpoints_ youll take an fault from the web browser owing the corresponding action orders or views are not useful.
_Author_Books
_Author_Articles Use endpoint routing to feel mysterious actions
We can explain this problem using routing — by creating a way that dynamically maps to an action that accepts a view name as a parameter and renders the view if a view with the specified name is establish.
In the Startup.cs file_ form a new way determination in the Conaspect order as shown in the code snippet given under.
endpoints.MapControllerRoutename: "viewName"_ model: "{controller}_{*viewName}"_ lapses: new { action _ "DisplayAnyView" };<_pre> The DisplayAnyView order looks like this:
open IActionResult DisplayAnyViewstring viewName{ recur ViewviewName; }<_pre> The AuthorController now looks like this:
open class AuthorController : Controller{ [HttpGet] open ActionResult Index { recur View; } open IActionResult DisplayAnyViewstring viewName { recur ViewviewName; } }<_pre> When you execute the application now_ youll remark that the fracture point is hit successfully as shown in the aspect under.
![]()
IDG<_little><_aspect> Use attribute routing to feel mysterious actions
You can also explain the problem using attribute routing as shown in the code snippet given under.
[HttpGet"DisplayAnyView"]open IActionResult DisplayAnyViewstring viewName { recur ViewviewName; }<_pre> Here is the complete code listing of the AuthorController class for your relation.
[Route"[controller]"]open class AuthorController : Controller { [Route"[action]"] [HttpGet] open ActionResult Index { recur View; } [HttpGet"DisplayAnyView"] open IActionResult DisplayAnyViewstring viewName { recur ViewviewName; } }<_pre> Lastly_ you should call the MapControllers order as shown in the code snippet given under to empower attribute-based routing.
app.UseEndpointsendpoints _gt;{ endpoints.MapControllerRoute name: "lapse"_ model: "{controller_Home}_{action_Index}_{id?}" endpoints.MapControllers; };<_pre> An action order is a open and non-static order of a controller class that can feel incoming requests and is invoked based on an action. The ASP.NET Core MVC engine is expert at redirecting an incoming request to the corresponding action order. However_ if there is no action order that matches the request_ a runtime fault will befall.
How to do more in ASP.NET Core:
- How to overload action orders in ASP.NET Core 5 MVC<_li>
- How to use multiple instrumentations of an interface in ASP.NET Core<_li>
- How to use IHttpClientFactory in ASP.NET Core<_li>
- How to use the ProblemDetails middleware in ASP.NET Core<_li>
- How to form way constraints in ASP.NET Core<_li>
- How to feel user secrets in ASP.NET Core<_li>
- How to build gRPC applications in ASP.NET Core<_li>
- How to redirect a request in ASP.NET Core<_li>
- How to use attribute routing in ASP.NET Core<_li>
- How to pass parameters to action orders in ASP.NET Core MVC<_li>
- How to use API Analyzers in ASP.NET Core<_li>
- How to use way data tokens in ASP.NET Core<_li>
- How to use API versioning in ASP.NET Core<_li>
- How to use Data Transfer Objects in ASP.NET Core 3.1<_li>
- How to feel 404 faults in ASP.NET Core MVC<_li>
- How to use dependency injection in action filters in ASP.NET Core 3.1<_li>
- How to use the options model in ASP.NET Core<_li>
- How to use endpoint routing in ASP.NET Core 3.0 MVC<_li>
- How to ship data to Excel in ASP.NET Core 3.0<_li>
- How to use LoggerMessage in ASP.NET Core 3.0<_li>
- How to send emails in ASP.NET Core<_li>
- How to log data to SQL Server in ASP.NET Core<_li>
- How to schedule jobs using Quartz.NET in ASP.NET Core<_li>
- How to recur data from ASP.NET Core Web API<_li>
- How to format response data in ASP.NET Core<_li>
- How to use an ASP.NET Core Web API using RestSharp<_li>
- How to accomplish async operations using Dapper<_li>
- How to use component flags in ASP.NET Core<_li>
- How to use the FromServices attribute in ASP.NET Core<_li>
- How to work with cookies in ASP.NET Core<_li>
- How to work with static files in ASP.NET Core<_li>
- How to use URL Rewriting Middleware in ASP.NET Core<_li>
- How to instrument rate limiting in ASP.NET Core<_li>
- How to use Azure Application Insights in ASP.NET Core<_li>
- Using advanced NLog components in ASP.NET Core<_li>
- How to feel faults in ASP.NET Web API<_li>
- How to instrument global qualification handling in ASP.NET Core MVC<_li>
- How to feel null values in ASP.NET Core MVC<_li>
- Advanced versioning in ASP.NET Core Web API<_li>
- How to work with worker services in ASP.NET Core<_li>
- How to use the Data Protection API in ASP.NET Core<_li>
- How to use conditional middleware in ASP.NET Core<_li>
- How to work with session state in ASP.NET Core<_li>
- How to write efficient controllers in ASP.NET Core<_li> <_ul>