locked
The route template 'x' on the action named 'x' on the controller named 'x' cannot begin with a forward slash RRS feed

  • Question

  • User1236783433 posted

    I'm workin on a ASP.NET MVC project, and I use custom routing to controle routes in my web aplication, but I face a problem.

    when I try to use a custom route for an Action started with slash / , I got this error :

    The route template '/HELP/Fixed_Value' on the action named 'Fixed_Value' on the controller named 'Help' cannot begin with a forward slash.

    And when I remove the slash the problem solved, but I want to use slash before the route.

    The Help Controller With Fixed_Value Action Code I try :

    public class HelpController : Controller
    {
    
        public ActionResult Index()
        {
            return View();
        }
    
    
        [Route(HELP_Route.Fixed_Value)]
        public ActionResult Fixed_Value()
        {
            return View();
        }
    
    
    }

    The HELP_Route Class :

    public class HELP_Route
    {
        public const string AREA = "/HELP";
    
    
    
    
        public const string Fixed_Value = AREA + "/Fixed_Value";
    
    }

    Please any help to fix this issue ? and massive thanks in advance.

    Tuesday, June 16, 2020 7:38 PM

All replies

  • User1686398519 posted

    Hi,MBARK

    The code you gave is used, but your problem cannot be reproduced. I suggest you check your routing configuration file RouteConfig, or give your routing configuration information.

    This is my RouteConfig.

    public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }
        } 

    Best Regards,

    YihuiSun

    Wednesday, June 17, 2020 1:47 AM
  • User1236783433 posted

    Firstl thanks for your time, My route config is : 

        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapMvcAttributeRoutes();
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }
        }

    Please any help

    Wednesday, June 17, 2020 7:56 AM
  • User1686398519 posted

    Hi MBARK,

    The route template cannot start with a '/' or '~' character and it cannot contain a '?' character.

    You can modify the code like this.

    public const string AREA = "HELP";

    Best Regards,

     YihuiSun

    Thursday, June 18, 2020 6:50 AM