ASP.NET path does not receive the parameter in Action after being configured

2

I mapped a new route on my site like this:

routes.MapRoute(
            "PaymentEdit",
            "Payment/{type}",
            new { controller = "Contributor", action = "Payment" },
            new { type = UrlParameter.Optional }
);

My Action is this:

[AutorizacaoFilterAttribute]
public ActionResult Payment(string rt)
{
    Debug.WriteLine(rt);
    return View();
}

The controller is called Contributor, it's right. But when I call the url: http://localhost:54345/Contributor/Payment/1 'Debug' does not return anything in Output . I have done the same type of route in another Action and it works normal, in my view is the same thing, follow the code:

Route:

routes.MapRoute(
            "Home",
            "Image/{produtoid}",
            new { controller = "Home", action = "Image"},
            new { produtoid = UrlParameter.Optional }
);

Action:

public ActionResult Image(string id)
{  
        Debug.WriteLine(id);
        return View();
}

When I call url http://localhost:54345/Home/Image/2107209300 Debug , I usually return the 2107209300 value. I've tested it several times in several ways, and I can not find the difference between the two.

My complete Route.config:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace SiteTeste
{
    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 }
            );

            routes.MapRoute(
                "Home",
                "Image/{produtoid}",
                new { controller = "Home", action = "Image"},
                new { produtoid = UrlParameter.Optional }
            );

            routes.MapRoute(
                "Contributor",
                "Payment/{type}",
                new { controller = "Contributor", action = "Payment" },
                new { type = UrlParameter.Optional }
            );
        }
    }
}

EDIT: Changing the route and inserting it in first place in route.config, when I pass the url: link I have the following error:

  

The 'type' restriction entry on the route with the 'Contributor / Payment / {type}' URL must either have a string value or be of a type that implements IRouteConstraint.

Follow the edited route:

routes.MapRoute(
            "Contributor",
            "Contributor/Payment/{type}",
            new { controller = "Contributor", action = "Payment" },
            new { type = UrlParameter.Optional }
);
    
asked by anonymous 18.08.2017 / 16:06

1 answer

1

You were wrongly calling in your first question, the correct%% of the route created:

Payment/123
Payment/abc

and the other way you were trying to do was wrong and hit the default route url that does not have Default but type configured.

Reformatted configuration:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Home",
        "Image/{produtoid}",
        new {
            controller = "Home",
            action = "Image",
            productid = UrlParameter.Optional
        },
        new { produtoid = @"\w+" }
     );

    routes.MapRoute(
        "Contributor",
        "Payment/{type}",
        new {
            controller = "Contributor",
            action = "Payment",
            type = UrlParameter.Optional
        },
        new { type = @"\w+" }
     );

    routes.MapRoute(
        "Contributor_Payment",
        "Contributor/Payment/{type}",
        new
        {
            controller = "Contributor",
            action = "Payment",
            type = UrlParameter.Optional
        },
        new { type = @"\w+" }
     );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { 
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional 
            }
    );    
}

note that 1 additional route has now been created, so it will work

+------------------+----------------------+--------------------------------+
|   Controler      |      Action          |       Url                      |
+------------------+----------------------+--------------------------------+
|   Home           |       Image          |   Home/Image/123456            |
+------------------+----------------------+--------------------------------+
|   Contributor    |      Payment         |   Payment/123456               |
+------------------+----------------------+--------------------------------+
|   Contributor    |      Payment         |   Contributor/Payment/123456   |
+------------------+----------------------+--------------------------------+

The last two have fallen in the same id and Controller as a way to exemplify, another factor the route Action is always the last one and finally rules have been added in the route this prevents the execution of routes which do not match the rule, in the case itself will accept text on both routes, but, This can be limited to numbers or even custom rule settings .

18.08.2017 / 17:00