FAQ Database Discussion Community


Use just Controller name as route

asp.net,asp.net-mvc,asp.net-mvc-routing
I would like to access my NewsList route by that URL: http://domain.com/news I have three routes like: routes.MapRoute( name: "NewsList", url: "News", defaults: new { controller = "News", action = "List" } ); routes.MapRoute( name: "NewsDetail", url: "{controller}/{action}/{title}/{id}", defaults: new { controller = "News", action = "Details", title = "",...

MvcSiteMapProvider “Home” button in menu not working onclick

asp.net-mvc-5,asp.net-mvc-routing,mvcsitemapprovider
I have a menu generated with MvcSiteMapProvider using a helper for bootstrap and routing: Controller Home: [MvcSiteMapNode(Title = "Home", Key = "home")] public ActionResult Index() { return View(model); } Controller Other: [Route("mypageview", Name = "mypage")] [MvcSiteMapNode(Title = "My Page", ParentKey = "home", Key = "mypage")] public ActionResult mypage() { mymodel...

How to determine Route Prefix programmatically in asp.net mvc?

c#,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing,attributerouting
I wanted to provide some URL separation for my public/anonymous controllers and views from the admin/authenticated controllers and views. So I ended up using entirely Attribute Routing in order to take more control of my URLs. I wanted my public URLs to start with "~/Admin/etc." while my public URLs would...

Generating URLs for ASP.NET MVC Area routing with subfolder

c#,asp.net,asp.net-mvc,asp.net-mvc-routing,asp.net-mvc-areas
I have an ASP.NET MVC app which has areas with special routes as following: Area registration file: context.Routes.MapHttpRoute( "AccessControlApi_default", "accesscontrol/api/{controller}/{id}", new { id = RouteParameter.Optional } ); context.MapRoute( "AccessControl_dashboardwidgets", "accesscontrol/dashboardwidgets/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new[] { "AccessControl.Controllers.DashboardWidgets" } ); context.MapRoute( "AccessControl_default",...

Targeting same action method in other controllers while generating an outgoing URL

c#,asp.net-mvc,asp.net-mvc-routing
Let's say I have only this route: routes.MapRoute("MyRoute", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); We can see that our startup page will be Home/Index. And let's say I have created an anchor element using this code in the view: @Html.ActionLink("This targets another controller","Index",...

How to use the same route with different parameter types?

asp.net-web-api,asp.net-mvc-5,asp.net-mvc-routing,web-api,asp.net-web-api-routing
I have an Api controller with two different actions that take different parameter types. // GET: users/sample%40email.com [Route("users/{emailAddress}")] public IHttpActionResult GetUser(string emailAddress) // GET: users/1D8F6B90-9BD9-4CDD-BABB-372242AD9960 [Route("users/{reference}")] public IHttpActionResult GetUserByReference(Guid reference) Problem is multiple actions are found matching when I make a request to either. Looking at other answers I thought...

Routing in asp.net mvc with multiple optional parameters

c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing
I am developing a asp.net mvc 4 application where I need a custom route which will handle multiple parameters like this: routes.MapRoute( "Default", "FilteredResults/Filter1-{1stParam}/Filter2-{2ndParam}/..../Filter-N{nthParam}", // URL with parameters new { controller = "Home", action = "Index", id = "" } defaults ); I want to be able to pass only...

How to augment actionlink with route values at runtime with JavaScript in ASP.NET MVC 5?

javascript,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing
I have this ActionLink. @Html.ActionLink("Link", "action", "controller", null, htmlAttributes: new {@class = "menuLink"}) I have to set routeValues to null because I don't know the value at compiletime. They are recieved from the selectedvalue of some dropdowns at runtime. Hence, I am trying to augment the routevalues at runtime with...

Map Custom Route ASP.NET MVC5

c#,asp.net-mvc-5,asp.net-mvc-routing
I haven't used .NET Routing before. I have a URL: http://myurl.com/Account/Login/?IsIPA=true. I want to be able to hit this URL with the following:http://myurl.com/IPA This is the only custom route I want hit. Can I create a route just for a single URL like this? My code that isn't working is:...

Equivalent of RouteData.GetRequiredString in ASP.NET 5

asp.net,asp.net-mvc-routing,asp.net-5
I have several HtmlHelper extension methods that I use to create navbar buttons - one is for a context-sensitive help link. In my extension method I need to know the name of the current controller and action e.g.: var currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action"); var currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller"); What can I use...

What is Routedata.Values[“”]?

c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing,routedata
I am surprised to see that there is no article which answers this question with any details. I have few questions related to RouteData.Values[""]. I saw this code: public ActionResult Index() { ViewBag.Message = string.Format("{0}---{1}--{2}", RouteData.Values["Controller"], RouteData.Values["action"], RouteData.Values["id"]); return View(); } Here it is basically reading values which potentially sounds...

Adding 1000 routes to MVC5 application

asp.net-mvc,asp.net-mvc-routing
To avoid showing the ID of a webpage in the URL I'm thinking of adding routes which can help figure out the ID based on the URL text. In other words, Do Not Want: /pages/3232/some-webpage Prefer: /pages/some-webpage (where some-webpage will mean ID=3232) Will it be an overkill if I add...

ASP.NET MVC4 - Different routing for 2 controllers with actionLinks

asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing,actionlink,html.actionlink
Okay, I've got this case where I have two controllers: HomeController MathController I want the routing for my HomeController to stay as default:{controller}/{action}/{id}. But I want to access the actions in the MathController with http://myurl/Task/Math/{action}. So what I've done is to write my RouteConfig like this: routes.MapRoute( name: "Math", url:...

Serving extensionless static files in ASP.NET MVC; getting 404s

asp.net,asp.net-mvc,iis,asp.net-mvc-routing
For whatever reason (ugh, just assume we have to), we have a JavaScript file and a CSS file without an extension in our ASP.NET MVC application. The JavaScript file is at path ~/Scripts/js and the CSS file is at ~/Styles/css. These are static files containing JS and CSS respectively, but...

Possible to create catch all route for a controller?

c#,asp.net-mvc,asp.net-mvc-routing
For a specific controller, is it possible to route any action that does not exist to the index? For example if I have fashionController/ fashionController/shoes/ fashionController/bags/ fashionController/otherStuff/ I want to be able to only setup the Index view & action and that anything else will just use the Index automatically...

routing in asp.net mvc gives me lenght

asp.net,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing
I'm trying to create this route: http://localhost:28790/Admin/Reporting/Reporting?reportName=MyReportName In order to access to this Controller: public ActionResult Reporting(string reportName){...} For this, i've added this routing in the area: context.MapRoute( "Admin_Reporting", "Admin/Reporting/Reporting/{reportName}", new { reportName = UrlParameter.Optional } ); And I've tested this ActionLink @Html.ActionLink(My Link, "Reporting", "Reporting", new { area =...

RegisterRoutes in ASP.NET MVC 4

asp.net-mvc-routing
I have two areas Admin and SecurityGuard. Routing shown below. Displays error: System.ArgumentException: The route with the name "Admin_default" is already in the family route. Names must be unique routes. Parameter name: name RouteConfig: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller...

Pass multiple parameters to C# MVC action without knowing the exactly number?

c#,asp.net-mvc,asp.net-mvc-routing
Is it possible to pass multiple parameters to C# MVC action without knowing the exactly number? For example, I could pass mywebsite.com/brands/gm/ford/fiat/dodge OR mywebsite.com/brands/gm/ford/fiat However, I don't want to have to create a route like: "brands/{brand1}/{brand2}/{brand3}/{brand4}" Because a variable number of brands will be passed to it. How can I...

MVC Routing - Generate the Route URL With Good Coding Standards

asp.net-mvc,asp.net-mvc-routing
I'm learning how to do routing in MVC. It seems to me that the routing API only solves half the problem. I can easily see how to map incoming URLs to controller actions and params. However, it is not obvious to me how to generate these routed URLs in the...

how to overload controllers in MVC

c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-routing
I am trying to overload the MVC controllers, how can I overload it properly? I want to list all companies in my website in a ListCompanies() controller like below http://localhost:21047/Home/ListCompanies and I want to add search criteria if user makes a search like below http://localhost:21047/Home/ListCompanies/sera "sera" is my search criteria....

What is the method for generating urls using RouteLink when a route name is not supplied?

asp.net,asp.net-mvc,asp.net-mvc-routing
Using @Html.RouteLink with a route name simply directs the routing engine to use the route of that name. This is easy to understand and also apparently the only way anyone who writes a blog entry or the documentation attempts to explain RouteLink. I appreciate that this is the preferred method...

Multiple controller route path in MVC

asp.net-mvc-routing
I have a mvc application Areas folder structure of the following type: Areas => FolderA => Controllers => ControllerA & ControllerB I have used the following register path in AreaRegistration: context.MapRoute( "default1", "FolderA/{controller}/{action}/{id}", new { controller = "ControllerA|ControllerB", action = "Index", id = UrlParameter.Optional } ); and I have two...

UrlParameter is always null in MVC

c#,asp.net-mvc,asp.net-mvc-routing
I cannot get the UrlParameter's value in MVC. I think everything is setup correctly except 1 thing. here is my MapRoute public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Banyo", // name it! "{controller}/{action}/{Filtre}", // Route name new { controller = "Banyo", action = "Marka", Filtre = UrlParameter.Optional } //...

Can I apply routing in ASP.NET MVC only to certian actions?

asp.net-mvc,routing,asp.net-mvc-routing
I have many actions in my HomeController, and I want to apply a specific routing to a few of them. For example, if the action name is Life, Health or Sport, I want to call them like Category/Sport, etc. But if the action name is something else, I want to...

MVC Routes, adding a filter to existing page

asp.net-mvc,asp.net-mvc-routing,actionlink
I have a route: context.MapRoute( "ISPCCodeSearch_default", "OnlineOrder/{controller}/ISPCCodeSearch/{ISPC}", new { area = "OnlineOrder", controller = "Home", action = "ISPCCodeSearch", ISPC = UrlParameter.Optional, }); that brings up a number of products by a product code eg, OnlineOrder/Home/ISPCCodeSearch/11011/ I want to further filter this by brand by clicking on a filter link on...

MVC 5 routing issue with new url for subcategories

asp.net-mvc-5,asp.net-mvc-routing,razor-2,asp.net-mvc-viewmodel
In my productcontroller I have two actionresult returning methods: [Route("Shop/{brand}/{category}/{subcategory?}/{page:int?}")] public ActionResult Index(string brand, string category, string subcategory, int? page, SortOptions currentSort = SortOptions.SinceDesc) { //... and [HttpPost] [Route("Shop/{brand}/{category}/{subcategory?}/{page:int?}")] public ActionResult Index(ProductsViewModel pvm) { //... And this is my razor view: @using (@Html.BeginForm("Index", "Products", FormMethod.Post)) { @Html.DropDownListFor(x => x.SubCatID,...

Hiding Get-Action in Web API (mvc)

asp.net-mvc-routing,asp.net-web-api2
I have a web api with the following routes: routes.MapHttpRoute( name: "DefaultGet", routeTemplate: "api/{controller}/{id}", defaults: new { controller = "Home", id = RouteParameter.Optional } ); routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = RouteParameter.Optional } ); These are two Actions inside a...

Action Name Attribute Not Linking to Correct View()

asp.net-mvc-4,asp.net-mvc-routing,method-overloading
I'm Trying to Overload an ActionResult in MVC(4) so that It can return the same view. [ValidateInput(false)] public ActionResult SearchQuery(string SearchTerm, bool isAdvanced = false) { return View(new SearchViewModel(SearchTerm, 50, 0, -1, false, 0, null, isAdvanced)); } [ActionName("BatchSearchQuery")] [ValidateInput(false)] public ActionResult SearchQuery(SearchViewModel SVM) { return View(SVM); } However when I...

In RouteConfig, how can I IgnoreRoute for this “Rejected-By-UrlScan” path?

c#,asp.net,asp.net-mvc,asp.net-mvc-routing
I'm trying to apply an IgnoreRoute to a path that contains "Rejected-By-UrlScan", and don't understand the syntax. The MSDN documentation here provides no examples. https://msdn.microsoft.com/en-us/library/system.web.mvc.routecollectionextensions.ignoreroute(v=vs.118).aspx Here is an example path that I'm dealing with: http://example.com/Rejected-By-UrlScan?~/Content/fonts/glyphicons-halflings-regular.woff2 These are some IgnoreRoute examples that I've picked up, but all of these are directly...

RouteCollection.MapRoute not appearing when passing into a method

c#,asp.net-mvc-5,asp.net-mvc-routing
RouteConfig: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { // Bootstrap routes routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); // Field routes FieldRoutes fieldRoutes = new FieldRoutes(); fieldRoutes.DefineCreate( ref routes ); fieldRoutes.DefineDetails( ref routes ); fieldRoutes.DefineEdit( ref routes ); fieldRoutes.DefineDelete( ref routes ); // ... ICRUDRoutes interface: public interface ICRUDRoutes { void DefineCreate( ref...

how do I map my text box string into my url route?

asp.net-mvc,asp.net-mvc-routing
I have a text box that I enter data into and pass with a post to my 'home' controller on action 'results' I want the url to end up looking like this when I post back https://localhost:44301/Home/Results/San Francisco, CA, United States I'm passing the text box data like this. @using...

How to handle relative routing in MVC?

asp.net-mvc,asp.net-mvc-4,kendo-ui,asp.net-mvc-routing
I am new to MVC and I have one particular route in my system which is default: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional } ); Now, since it is defaulting to Employee/Index, both localhost:// and localhost://Employee/Index are going to...

cannot set Default route for MVC project

c#,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing
I cannot set the Default Route for my MVC project. When I run the project, it goes to the address http://localhost:7555/ and it give the "HTTP ERROR 404" but if I enter the url http://localhost:7555/Default , it goes to the Default page. But I want users to go the Default...

.net mvc routing URL

asp.net-mvc,asp.net-mvc-routing
I have this in my RouteConfig.cs routes.MapRoute( name: "client1", url: "client1/{controller}/{action}", defaults: new { controller = "Home", action = "Index" } ); routes.MapRoute( name: "client2", url: "client2/{controller}/{action}",/* updated parent folder to match route name */ defaults: new { controller = "Home", action = "Index" } ); routes.MapRoute( name: "Default", url:...

RedirectToAction with parameters in Route

c#,asp.net-mvc,asp.net-mvc-routing
I have some problem with redirecting to some action basicly if I have no paramers on route or parameter which calls Id it works fine, yet when i add some different parameters to route RedirectToAction does not work, it throws an error: No route in the route table matches the...

Get controller and action from routeName

c#,asp.net-mvc-5,asp.net-mvc-routing
I am trying to construct a extension method which can highlight the navigation item which is currently viewed. All my links are generated using a standard Url.RouteUrl("RouteName") - this generates a fully qualified link for the controller and action. Now I need to extract the controller and action from this...

MVC5 Routing with two parameters in an Area

c#,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing
I have an area called News, and within that area I have a Post Controller. The post controller has the following action: public ActionResult Index(int id, string name) { ... } I have a route in the NewsAreaRegistration context.MapRoute( "News_post", "News/{controller}/{id}/{name}", new { action = "Index", controller = "Post",name =...

Understanding MVC routing conventions

asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing
I am looking at an example of MVC Route and it looks like following: routes.MapRoute("Cuisine", "cuisine/{name}", new { controller = "Cuisine", action = "Search", name = ""}) I am just trying to dissect what each element stands for here: "cuisine/{name}" . Does this part say that if a request comes...

Redirect to action based on role not working. Maybe routing needed?

c#,asp.net,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing
I have the following controller public class GlobalAdminController : Controller { // GET: GlobalAdmin [AuthorizeUser(Roles = "admin")] public ActionResult Index() { return View(); } } And the home controller which is the main landing page for the app public ActionResult Index() { if(User.IsInRole("admin")) { RedirectToAction("Index", "GlobalAdmin"); } return View(); }...

MVC Routing News Pages

asp.net-mvc-4,asp.net-mvc-routing
I have managed to get my MVC project to present a list of news items in an SEO friendly manner: /News/ - to present the list /News/NewsItem/id/news-item-title - the individual news item What I would really like is: News/id/news-item-title Exactly how Stackoverflow presents its questions. However, I cant seem to...

Is there any way to pass dynamic parameter with route attribute?

asp.net-mvc,url,asp.net-mvc-routing
I am working on CMS and need to do dynamic routing. I tried a lot of things but I decided to use routing like below. [Route("{leftPath:values(announcements|duyurular)}/{announceUrl?}")] public ActionResult Announcements(string leftPath, string announceUrl) { ViewBag.Url = announceUrl; return View(); } Problem: [Route("{leftPath:values(announcements|duyurular)}/{announceUrl?}")] bold part is dynamic. For example, if I use...

Web API routing and a Web API Help Page: how to avoid repeated entries

asp.net-web-api,asp.net-mvc-routing,web-api,asp.net-web-api-helppages
I am getting repeat entries rendered in my Web API Help Page with different parents, such as these, that refer to the same method: GET api/{apiVersion}/v1/Products - Gets all products ... GET api/v1/Products - Gets all products ... I have a Web API page with some routing like this: config.Routes.MapHttpRoute...

How does a request URL “HelloWorld” instantiate a HelloWorldController object in ASP.Net MVC?

c#,asp.net-mvc,controller,asp.net-mvc-routing
I'm finding this documentation very confusing. After creating a file using System.Web; using System.Web.Mvc; namespace MvcMovie.Controllers { public class HelloWorldController : Controller { // // GET: /HelloWorld/ public string Index() { return "This is my <b>default</b> action..."; } // // GET: /HelloWorld/Welcome/ public string Welcome() { return "This is the...

Can I use Ninject when setting up my routes?

asp.net-mvc,asp.net-mvc-routing,ninject,ninject.web.mvc
I have an ASP.NET MVC5 web site that uses Ninject. The site is for an online magazine. What I would like to do is set up some routes, but pull the data from an injected repository. So, I would like to have routes like these... routes.MapRoute("Section9", "Section9_Ask-the-professional", new {controller =...

MVC 5 Routing with parameter using @Url.RouteUrl

asp.net-mvc,routing,asp.net-mvc-5,asp.net-mvc-routing
I have an ActionResult that I have applied a routing attribute to: [Route("myproduct/{productID}", Name = "myproduct")] [MvcSiteMapNode(Title = "Products", ParentKey = "products", Key = "myproducts")] public ActionResult myproducts(int productID) ... I am trying to link to the view via a RouteUrl: <a href="@Url.RouteUrl("myproducts", @Model.myproducts[i].productID)">Buy</a> The resulting html does not even...

How To Make a Wild Card Route of Sorts with ASP.NET MVC

asp.net-mvc,asp.net-mvc-routing
I want to map all calls that look like mydomain.com/ng* to my Ng Controller index. I'm not getting how to add that MapRoute. I've tried what is below but that give me an immediate error The route parameter name '' is invalid. // ng* routes.MapRoute("NgWildcard", "Ng{*}", new { /* Your...

C# ASP.NET MVC routing - route for all controllers with specific suffix

c#,asp.net-mvc,asp.net-mvc-routing
I have several controllers for widgets named {WidgetName}WidgetController, e.g. SampleWidgetController. I need to create a route which catches all requests to such controllers and passes them to one common controller together with requested controller's name and action. public class SampleWidgetController : Controller { public ActionResult Content() { ... } }...

MVC route attribute no controller

asp.net-mvc,asp.net-mvc-5,asp.net-mvc-routing
I'm building an intranet where I have the following home controller: [Route("{action=index}")] public class HomeController : Controller { public ActionResult Index() { return View(HomeModelBuilder.BuildHomeModel()); } public ActionResult FormsHome() { return View(HomeModelBuilder.BuildFormsHomeModel()); } } I'm trying to get my forms homepage to have a url of http://intranet/forms so I thought I...

WebApiConfig.Register Clears routes defined by RouteConfig.RegisterRoutes apon deployment

asp.net-mvc,asp.net-mvc-4,iis,asp.net-mvc-routing,asp.net-web-api-routing
I'm on struggle street here, When I try to add API controllers it seems to destroy all my MVC base routes and area routes. On my application start I call protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); Util.RazorDebuggingSetup(); BundleConfig.RegisterBundles(BundleTable.Bundles); } using System.Web.Mvc; An example of an area config: namespace...