0

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


}

6
  • 3
    Your code code is correct. If its not working, its most likely because you routes are in the wrong order Commented Jul 5, 2018 at 0:29
  • Show RouteConfig.cs Commented Jul 5, 2018 at 0:30
  • I'll post routeconfig.cs what order does it need to be in? Commented Jul 5, 2018 at 0:31
  • 2
    www.mysite.com/user/transactions/sometype should match for the TransactionRoute. Also I see no need for the title constraint. Commented Jul 5, 2018 at 0:48
  • yes, well, then why does sometype in the action keep showing a null value? Commented Jul 5, 2018 at 0:52

1 Answer 1

4

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" }   
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.