chuckd
chuckd

Reputation: 14520

Creating a ASP.Net MVC route with just forward slashes

I have a action in a controller that looks like this

public ActionResult Transactions(string type) {}

To access this controller and pass in a type property value I have to type

www.mysite/controller/transactions?type=sometype

but what I want is to pass something like this

www.mysite.com/controller/transactions/sometype

So I create a route config param in the RouteConfig.cs file like this

routes.MapRoute(
   name: "TransactionRoute",
   url: "user/transactions/{type}",
   defaults: new { controller = "user", action = "transactions", type = "made" },
   constraints: new { title = @"^[A-Za-z]+$" }
);

but now if I pass a url like this

www.mysite.com/controller/transactions/made

the value of the string type in the action is null

Am I allowed to do this or did I do something wrong?

Here is my routeconfig.cs file

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

  routes.MapRoute(
    name: "TransactionRoute",
    url: "user/transactions/{type}",
    defaults: new {
      controller = "user", action = "transactions", type = "made"
    },
    constraints: new {
      title = @ "^[A-Za-z]+$"
    }
  );

  routes.MapRoute(
    name: "RateRoute",
    url: "rate/event/{id}",
    defaults: new {
      controller = "rate", action = "event"
    },
    constraints: new {
      id = @ "\d+"
    }
  );

  routes.MapRoute(
    name: "ReviewRoute",
    url: "rate/review/{id}",
    defaults: new {
      controller = "rate", action = "review"
    },
    constraints: new {
      id = @ "\d+"
    }
  );

  routes.MapRoute(
    name: "SpaceCleanRoute",
    url: "space/{id}",
    defaults: new {
      controller = "space", action = "index", id = UrlParameter.Optional
    },
    constraints: new {
      id = @ "\d+"
    }
  );

  routes.MapRoute(
    name: "SpacePendingRoute",
    url: "space/{id}/{pending}",
    defaults: new {
      controller = "space", action = "index", pending = UrlParameter.Optional
    },
    constraints: new {
      id = @ "\d+"
    }
  );


  routes.MapRoute(
    name: "PublicSpaceRoute",
    url: "space/public/{title}",
    defaults: new {
      controller = "space", action = "public"
    },
    constraints: new {
      title = @ "^[A-Za-z0-9-]+$"
    }
  );

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


}

Upvotes: 0

Views: 598

Answers (1)

Nkosi
Nkosi

Reputation: 246998

www.mysite.com/user/transactions/sometype should match for the TransactionRoute.

Also I see no need for the title constraint based on the route template.

Remove the title constraint

routes.MapRoute(
   name: "TransactionRoute",
   url: "user/transactions/{type}",
   defaults: new { controller = "user", action = "transactions", type = "made" }   
);

Upvotes: 4

Related Questions