Map api route to mvc route - c#

I want my FilesController to be accesible from both /files/{action} and /api/files/{action}. I tried
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
but it not working. I deleted WebApiConfig, so in App_Start is only MVC RouteConfig, but it doesnt help.
Upd
controller code
public class FilesController : Controller {
public ActionResult Index() {
return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
}
}
request urls:
/files/index -> OK (200)
/api/files/index -> Not Found (404)
Upd #2
Complete RouteConfig.cs
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(
name: "FilesRoute",
url: "Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
}
}

I think you can try to add a new route in front of your original route setting.
This setting will first check URL whether is match Files/{action}/{id} , if not the use api/Files/{action}/{id}
routes.MapRoute(
name: "FilesRoute",
url: "Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
EDIT
The problem is route setting order you need to set the default route to be the last one.
/api/files/index => Otherwise, the setting will find api controller and files action.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "FilesRoute",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Home/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

Related

How to use multiple route in mvc5

Check the RouteConfig.cs file code bellow on mvc5 project. The first Default route which is configured for return home page is works fine. But the second one which i made to send traffic to Product controller is not working. The way i am trying to hit the controller is- http://localhost:50070/Product/somepage/good-product
The error i am getting is:
The resource cannot be found.
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(
name: "Product",
url: "Product/{pagename}/{slug}",
defaults: new { controller = "Product", action = "Index", slug = UrlParameter.Optional },
namespaces: new[] { "Demo.Controllers" }
);
}
}
You problem is in order of route mappings. Routes are matched in mapping order - in your case Product/somepage/good-product matches default route template and it is chosen. But you don't have action Somepage on Product controller.
Default route mapping should be the last mapping (just change the order):
routes.MapRoute(
name: "Product",
url: "Product/{pagename}/{slug}",
defaults: new { controller = "Product", action = "Index", slug = UrlParameter.Optional },
namespaces: new[] { "Demo.Controllers" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Please change second routes like that
Use controller in order to pagename
routes.MapRoute(
name: "Product",
url: "Product/{controller}/{slug}",
defaults: new { controller = "Product", action = "Index", slug = UrlParameter.Optional },
namespaces: new[] { "Demo.Controllers" }
);

Hiding Action name from URL

My url is this,
localhost:45678/Blogs/Index
I need it like this format
localhost:45678/Blogs
i write below code in route file but,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Blog",
url: "{Blog}",
defaults: new { controller = "Blog", action = "Index" }
);
routes.MapRoute(
name: "forblogcontent",
url: "Blog/{slug}/{id}",
defaults: new
{
controller = "Blog",
action = "DisplayContent",
slug = UrlParameter.Optional
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Content", action = "AddContent", id = UrlParameter.Optional }
);
}
When i click on an link button it still shows
localhost:45678/Blogs/Index
My link button code is
Blogs
How to correct this?

ASP.NET MVC 5 Traditional Routing

When I debug it, the Product and Subcategory link works fine, however the Category shows me the list but when I click on one of them to show me the products inside each one, does not display anything.
Here is my ProductsController.cs.
public ActionResult Index(string category, string subcategory, string search, string sortBy, int? page){... }
On the RouteConfig.cs I have:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ProductsCreate",
url: "Products/Create",
defaults: new { controller = "Products", action = "Create" }
);
routes.MapRoute(
name: "ProductsbySubCategorybyPage",
url: "Products/{subcategory}/Page{page}",
defaults: new { controller = "Products", action = "Index" }
);
routes.MapRoute(
name: "ProductsbyCategorybyPage",
url: "Products/{category}/Page{page}",
defaults: new { controller = "Products", action = "Index" }
);
routes.MapRoute(
name: "ProductsbyPage",
url: "Products/Page{page}",
defaults: new { controller = "Products", action = "Index" }
);
routes.MapRoute(
name: "ProductsbySubCategory",
url: "Products/{subcategory}",
defaults: new { controller = "Products", action = "Index" }
);
routes.MapRoute(
name: "ProductsbyCategory",
url: "Products/{category}",
defaults: new { controller = "Products", action = "Index" }
);
routes.MapRoute(
name: "ProductsIndex",
url: "Products",
defaults: new { controller = "Products", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Your ProductsbyCategorybyPage is overwritten by ProductsbySubCategorybyPage.
When ASP.NET is trying to parse the incoming URL, it will stop with the find match, and URL like Products/A/Page3 will be passed through the ProductsbySubCategorybyPage route. Routing module does not know what do you prefer A to be, subcategory or category. You need to refactor your RegisterRoutes method to use unique route masks. Like Products/SubCategory/{subcategory} and Products/Category/{category} for example.

Unable to Configure Routes Properly in my ASP.NET MVC app

I am configuring my routes in asp.net mvc app where there are multiple routes and only first and last route works but the routes defined in the center of my route.config file are not working. Following is the code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Doctor",
url: "admin/doctor/{action}/{id}",
defaults: new { controller = "admin", action = "AddDoctor", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Admin",
url: "admin/{action}/{id}",
defaults: new { controller = "admin", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
It seems that you forgot the controller part of your Admin route.
You should define it as below:
routes.MapRoute(
name: "Admin",
url: "admin/{controller}/{action}/{id}",
defaults: new { controller = "admin", action = "Login", id = UrlParameter.Optional }
);

MVC4 controller/route not working

my VisitsController:
public ActionResult Index(Visits visits, int? id)
{
....
return View(v);
}
Here is my route config
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 }
);
}
Works: http://localhost:49225/Visits
Does not work: http://localhost:49225/Visits/123
What would the custom route be (and force it to be an integer)?
You need to add a new route for that to work.
Currently, this will work:
/Visits/Index/123
I believe you need to add the following:
routes.MapRoute(
name: "VisitsDefault",
url: "Visits/{id}",
defaults: new { controller = "Visits", action = "Index",
id = UrlParameter.Optional }
);
This is assuming you have a modelbinder for Visits already.

Categories

Resources