ASP.NET route does not receive the parameter in Action after being set

Asked

Viewed 160 times

2

I Mapeed 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 doesn’t return anything in Output. I’ve done the same kind of route in another Action and 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 the url http://localhost:54345/Home/Image/2107209300 the Debugme return normally the value 2107209300 I’ve tried it several times in different ways, and I can’t 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 on route.config, when I pass the url: http://localhost:54345/Contributor/Payment/41242 I have the following error:

The type constraint entry on the route with the 'Contributor/Payment/{type}' URL must 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 }
);
  • 1

    What do you call views are, in fact, actions.

  • @LINQ Yes, these are the actions

  • 1

    Post your file RouteConfig.cs full. Taking advantage, try to put the payment route before the others.

  • It is with certainty shock of routes, post the complete file of RouteConfig.cs

  • Personal edited !

  • @Leonardobonetti the name route Home the produtoid is of what type

  • @Virgilionovic string, as well as the type of Contributor

Show 2 more comments

1 answer

1


You were calling wrong on your first doubt, the url correct for the created route:

Payment/123
Payment/abc

and the other way I was trying to do was wrong and fall into the standard route Default that does not have type and yes id configured.

Reworked 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 latter two fell into the same Controller and Action as a way of exemplifying, another factor the route Default is always the last and finally has been added rules on the route that prevents running routes that do not match the rule, in the case itself will accept text on the two routes, but, this may be limited to numbers or even custom rule settings.

References

  • 1

    Dude, I don’t believe that all this time (4 months) I believed that the Image route worked, even, I had this doubt, because I get "productoid" being that in the Image view I get a parameter called id and it works. Before your answer I had managed to correct the problem, including I will answer my question with my solution, but yours is the most correct, +1.

  • Just answer me one thing, what is \w+ ?

  • Regular expression to limit that phrase to a text, that is, it accepts any text, if it can limit for example it only accepts numbers, etc ... is a rule in the url that is good, @Leonardobonetti, gives a read in Route Constraints that has a link in the answer.

  • 1

    I understand, I will read yes with certainty ! Thank you Virgilio, I also did not know what this Constraint, I thought it was the variable itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.