Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

Asp.Net Core MVC: Create Alternative URL Routes

 

I would like in my Asp.Net Core MVC application for the list a different URL label than the default label.

For Asp MVC applications, the path is automatically set as follows:

Domain / Controller / Index

In the example this would be: https://codedocu.com/note/index

 

For the application, I would like to put the list of all results in the plural path / Notes with a -s and the list is split by index on Index_All and Index_Own.

 

Without routing

If the route or routing is not set in Asp.Net MVC applications then the input will show: a blank page

 

Standard URL route:

In the standard ASP.Net MVC the route is routed according to the pattern: Domain / Controller / Action / ID.

If there is no action method index, then the browser response remains empty.

 

If you have written a new method and you want it to be called, you put a so-called routing attribute before the method.

[Route("Index")]

[Route("Index_All")]

public async Task<IActionResult> Index_All()

..

 

Route("..") attributes

Keep in mind that there are two route attributes to define in an Asp.Net MVC controller.

The first attribute is prefixed as a route for the controller to the public class.

The second attribute is described in the public action and defines the controller / action route

 

 

Move from / Note to / Notes

 

namespace IWois_Core.Controllers

{

 

    [Route("Notes")]

    public class NoteController : Controller

    {

        private readonly ApplicationDbContext _dbContext;

 

        public NoteController(ApplicationDbContext context)

        {

            _dbContext = context;

        }

 

        // GET: /Note Root

        [Route("Index")]

        [Route("Index_All")]

        public async Task<IActionResult> Index_All()

        {

            //-------------< Index /Notes_All >-------------

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDUser = await Userinfos.UserInfo_Methods.getIDUser_as_Number(this.User,_dbContext ) ;

            //--</ Get User ID >--

 

            //< content_logger >

            int? sumLogViews=0;

            sumLogViews = await counter_Note_List_erhoehen(IDUser);

            //</ content_logger >

 

 

            ..

..

 

 

            //< out to view >

            return View(data_to_View);

            //</ out to view >

            //-------------</ Index /Notes All >-------------

        }

 

        // GET: /Note Own Root

        [Route("Index_Own")]

        public async Task<IActionResult> Index_Own()

        {

            //-------------< Index /Notes >-------------

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            long IDUser = await Userinfos.UserInfo_Methods.getIDUser_as_Number(this.User, _dbContext);

            //--</ Get User ID >--

 

            //< content_logger >

            int? sumLogViews = 0;

            sumLogViews = await counter_Note_List_erhoehen(IDUser);

            //</ content_logger >

 

 

Error messages:

Further error message with wrong routing setting

 

Error Cause:

The route must not contain a square brace. Square brackets are used only for [Controller] and [Action]

[Route("[Index_Own]")]

public async Task<IActionResult> Index_Own()

{..

 

 

An unhandled exception occurred while processing the request.

InvalidOperationException: The following errors occurred with attribute routing information:

 

Error 1:

For action: 'IWois_Core.Controllers.NoteController.Index_Own (IWois_Core)'

Error: While processing template 'Notes/[Index_Own]', a replacement value for the token 'Index_Own' could not be found. Available tokens: 'action, controller'. To use a '[' or ']' as a literal string in a route or within a constraint, use '[[' or ']]' instead.

Microsoft.AspNetCore.Mvc.Internal.ControllerActionDescriptorBuilder.Build(ApplicationModel application)

Mobile
»
Asp core redirect http to https and www. To directly domain host
»
Asp MVC Core: URL Query Parameters
»
Asp Core MVC: New Define Startup File, Start Controller View
»
Asp MVC: HTML links instead of server side links
»
AmbiguousActionException: Multiple actions matched
»
Asp.Net Core MVC: Create Alternative URL Routes

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de