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 }
);