Asp .net MVC 5 Route Attribute with id between route - c#

I´m trying to set up a route in MVC so that when POSTing to the following url
/organizations/55/repositories
I get all the repositories for organization 55
I've tried using the following route but to no avail, it never reaches the controller action method
[Route("/organizations/{id}/repositories")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Repositories(long id, OrganizationSearchParametersDTO parameters)
However if I do it in the RegisterRoutes method, it works:
routes.MapRoute("OrganizationControllerRoute", "organizations/{id}/repositories", new {controller = "Organizations", action = "Repositories"});
But I'd prefer to have it running using attributes because it's our way to work
What am I doing wrong, any ideas?

If your routes.MapRoute(..) definition works, but not the [Route(...)] attribute, it means that you have not enabled attribute routing in the RouteConfig.cs file
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Add the following line before any route definitions
routes.MapMvcAttributeRoutes();
... // add routes.MapRoute(...) definitions as required
}
}

Related

Implementing Attribute Routing

I'm trying to get a handle on attribute routing in MVC.
Initially, the routing for my sitemap controller was as follows:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SitemapXml",
url: "sitemap.xml",
defaults: new { controller = "Sitemap", action = "Index" }
// Additional mappings...
}
}
That works fine. But then I tried commenting out the SitemapXml routing above and instead adding an attribute in my controller.
// GET: Sitemap
[Route("sitemap.xml")]
public ActionResult Index()
{
// Generate sitemap
}
I also added the following line at the end of RegisterRoutes:
routes.MapMvcAttributeRoutes();
But now when I navigate to domain.com/sitemap.xml, I get a page not found error.
Questions:
What steps am I missing to get my routing attribute to work?
Since mappings can now be specified in two places (as attributes or set directly in the RouteCollection), what happens when those two places contradict each other?
if you remove the extension .xml , your attribute routing will work perfectly. And its better to use the extension related code in action method.
Also make sure your route config looke like (routes.MapMvcAttributeRoutes(); should exist before default route)
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);

Is there any way to make a custom route in C# MVC? [duplicate]

This question already has answers here:
Routing in ASP.NET MVC, showing username in URL
(2 answers)
Closed 4 years ago.
I am new to C# MVC,
This is my default RouteConfig File,
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 }
);
}
}
When I run my project http://localhost:50382 it redirects the default home index method.
How can I build custom url like http://localhost:50382/somestringcode and call a specific ActionMethod. Something Like a custom route that points to specific ActionMethod and the stringcode will be passed as parameter.
public ActionResult Method1( string code)
{
return View();
}
What you are searching for is attribute routing. That means specifying URL routes explicitly.
First, you need to enable it
routes.MapMvcAttributeRoutes();
Then add Route attribute to desired action:
[Route("")]
public ActionResult Method1( string code)
{
return View();
}
Since code parameter is simple type, it will be searched in request URL.
You need to do something like this, where controller1 is the controller name that contains Method1, and make sure you place this first, and leave the original route config, so when it doesn't match this route, it will use default route.
Please note this is bad practice, and it will fail in case route is trying to access default action for a controller "Index" as #stephen mentioned in the comments below, and that's why I would suggest adding a prefix ex "Action1" to the route.
routes.MapRoute
(
name: "Method1",
url: "Action1/{code}",
defaults: new { controller = "controller1", action = "Method1", code = "default"}
);

MVC Conventional and Attribute routing not working together

I am using conventional routing on an ASP.Net MVC project and would like to enable Attribute routing in parallel. I have created the following but I am getting a 404 on the conventional route when enabling attribute routing
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Controller
[RoutePrefix("Registration")]
public class RegistrationController : Controller
{
[HttpGet]
[Route("Add/{eventId}")]
public ActionResult Add(int eventId)
{
}
}
Calling
http://localhost/Registration/Add/1
Works, while calling
http://localhost/Registration/Add?eventId=1
No longer works and responds with 404 NotFound
Should work if you make the {eventId} template parameter optional in the route template
[RoutePrefix("Registration")]
public class RegistrationController : Controller {
//GET Registration/Add/1
//GET Registration/Add?eventId=1
[HttpGet]
[Route("Add/{eventId:int?}")]
public ActionResult Add(int eventId) {
//...
}
}
The reason the two were not working is that the route template Add/{eventId} means that the route will only match if the {eventId} is present, which is why
http://localhost/Registration/Add/1
works.
By making it (eventId) optional eventid? it will allow
http://localhost/Registration/Add
to work as the template parameter is not required. This will now allow query string ?eventId=1 to be used, which the routing table will use to match the int eventId parameter argument on the action.
http://localhost/Registration/Add?eventId=1
I also got this issue. Which MVC version are you using?
I faced this issue with MVC in asp.net core.
I think this is a flaw as if you provide Routing attribute on any action method, its conventional route is over ridden and is not longer available so you get 404 error.
For this to work, you can provide another Route attribute to your action method like this. This will work
[Route("Add/{eventId}")]
[Route("Add")]

Action attribute that restrict action method by url pattern

Is there an action attribute that allows restricting action method by url pattern?
In my controller, I would like to to have two SearchOrder actions. One for editing order and one for viewing order. If the url path is /Order/EditOrder/SearchOrder/1, I would want it to execute this action.
[HttpGet]
[ActionName("SearchOrder")]
public ActionResult EditOrderSearchOrder()
{
. . . .
}
But if the url path is /Order/ViewOrder/SearchOrder/1, I would want it to execute this action.
[HttpGet]
[ActionName("SearchOrder")]
public ActionResult ViewOrderSearchOrder()
{
. . . .
}
There are many ways. Some of them are
Write in RouteConfig.cs
routes.MapRoute(
name: "Properties",
url: "Order/EditOrder/SearchOrder/{action}/{id}",
defaults: new
{
controller = "YourControllerName",
action = "SearchOrder",\\ bcoz you have given action name attribute otherwise your method name
id = UrlParameter.Optional \\ you can Change optional to Fixed to make passing Id parameter compulsory
}
);
If using Mvc5, you can do Attribute Routing by following a simple syntax:
[Route("Order/EditOrder/SearchOrder/{id?}")] // ? For optional parameter
public ActionResult EditOrderSearchOrder(){}
And
To enable attribute routing, call MapMvcAttributeRoutes in RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
routes.MapMvcAttributeRoutes();
}
}

Is it possible to define a parameter before action in url

The question is as simple as the title.
Is it possible to have a route that looks like this: {controller}/{id}/{action}?
This is what I have in code right now (just a simple function) (device is my controller):
[HttpGet]
[Route("Device/{id}/IsValid")]
public bool IsValid(int id) {
return true;
}
But when I go to the following URL the browser says it can't find the page: localhost/device/2/IsValid.
And when I try this URL, it works just fine: localhost/device/IsValid/2
So, is it possible to use localhost/device/2/IsValid instead of the default route localhost/device/IsValid/2? And how to do this?
Feel free to ask more information! Thanks in advance!
You are using Attribute routing. Make sure you enable attribute routing.
Attribute Routing in ASP.NET MVC 5
For MVC RouteConfig.cs
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
//...Other code removed for brevity
}
}
In controller
[RoutePrefix("device")]
public class DeviceController : Controller {
//GET device/2/isvalid
[HttpGet]
[Route("{id:int}/IsValid")]
public bool IsValid(int id) {
return true;
}
}
try using this before Default route in RoutingConfig
config.Routes.MapHttpRoute(
"RouteName",
"{controller}/{id}/{action}"
);

Categories

Resources