Hiding Action name from URL - c#

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?

Related

Map api route to mvc route

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

Issue related to routing in MVC

I have MVC webapplication. i have written following rules in Route.config file
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: "about",
url: "Home/About-WDI",
defaults: new { controller = "Home", action = "About"}
);
}
and in global.asax file, i have written following code:
RouteConfig.RegisterRoutes(RouteTable.Routes);
now problem is when i type http://localhost:60123/Home/About-WDI
it gives me error saying Resources not found
what is issue in above code?
Thanks
As already mentioned in the comments, the order of the routes is taken into concern. And as your {id} parameter is optional the URL will be matched by the first route. So simply exchange both routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "about",
url: "Home/About-WDI",
defaults: new { controller = "Home", action = "About"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

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

Mvc 5 supporting legacy ashx links

Is there a way to support ashx links as in MapRoute?
For example I have this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "ashx",
url: "test/redirect.ashx",
defaults: new { controller = "Click", action = "Redirect" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This doesnt work but what im looking for is, if the url starts is www.example.com/test/redirect.ashx?foo=bar it will redirect me to my controller ClickController.Redirect(). is this possible? Am i Doing this wrong?

Categories

Resources