locked
Please help me with the proper syntax for the below routing in ASP.NET Core RRS feed

  • Question

  • User2056817590 posted

    routes.MapRoute(name: "TOS", url: "TOS", defaults: new { controller = "Register", action = "TermsOfService" });
                routes.MapRoute(name: "About", url: "About", defaults: new { controller = "Home", action = "About" });
                routes.MapRoute(name: "EIRR", url: "EIRreport/{gkey}", defaults: new { controller = "EIR", action = "EIRreport", gkey = UrlParameter.Optional });
                routes.MapRoute(name: "EIRByGuid", url: "EquipmentInterchangeReceipt/{guid}", defaults: new { controller = "EIR", action = "EIRreportByGuid", guid = UrlParameter.Optional });

    Tuesday, May 4, 2021 5:09 AM

All replies

  • User-474980206 posted

    its all in the docs:

      https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0

    see MapControllers example. attribute routing is preferred. also routine binding is more strict in asp.net core, and you may get more routing errors than in classic asp.net for api controllers.

    Tuesday, May 4, 2021 3:02 PM
  • User287926715 posted

    Hi  kalvakrishna, 

    The default project has a MapRoute, for example:

    routes.MapRoute(
                "Default",
                "{controller}/",
                new { controller = "Home", action = "Index" }
                );

    You can also use custom routes:

    routes.MapRoute(
            name: "MyName",
            url: "{controller}/{action}/{myParam}",
            defaults: new { controller = "MyController", action = "MyAction", id = UrlParameter.Optional }
        );

    MyController:

    public ActionResult MyAction(string myParam = "")
    {
    
    }

    For more information, you can refer to the document.

    Best Regards,

    ChaoDeng

    Wednesday, May 5, 2021 7:11 AM
  • User2056817590 posted

    Asking the syntax in ASP.NET Core not for ASP.NET MVC 

    Wednesday, May 5, 2021 10:29 AM
  • User-474980206 posted

    the docs are clear. here is the sample in the docs

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(name: "blog",
                    pattern: "blog/{*article}",
                    defaults: new { controller = "Blog", action = "Article" });
        endpoints.MapControllerRoute(name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
    });

    which of your routes could you not convert?

    Wednesday, May 5, 2021 4:02 PM
  • User2056817590 posted

    Yes but when we want to define the optional parameter the syntax would be  How it is?

    for example for the following 

     routes.MapRoute(name: "EIRR", url: "EIRreport/{gkey}", defaults: new { controller = "EIR", action = "EIRreport", gkey = UrlParameter.Optional });

    Thursday, May 6, 2021 3:52 AM
  • User287926715 posted

    Hi  kalvakrishna,

    You can refer to this demo.

    Startup:

    app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}/{name?}");
                });

    HomeController:

      public IActionResult Index(int id, string name)
            {
                return View();
            }
    

    Result:

    You can refer to the documentation if you want more operations.

    Best Regards,

    ChaoDeng

    Thursday, May 6, 2021 8:24 AM
  • User-474980206 posted

    Why do you refuse to read the docs? In The routing link given to you is full of examples of optional parameters, and explains:

    Route parameters may have default values designated by specifying the default value after the parameter name separated by an equals sign (=). For example, {controller=Home} defines Home as the default value for controller. The default value is used if no value is present in the URL for the parameter. Route parameters are made optional by appending a question mark (?) to the end of the parameter name. For example, id?. The difference between optional values and default route parameters is: A route parameter with a default value always produces a value. An optional parameter has a value only when a value is provided by the request URL.

    Thursday, May 6, 2021 2:30 PM