asp.net-mvc-routing

How to prevent Url.RouteUrl(…) from inheriting route values from the current request

元气小坏坏 提交于 2019-11-30 04:20:19
Lets say you have an action method to display products in a shopping cart // ProductsController.cs public ActionMethod Index(string gender) { // get all products for the gender } Elsewhere, in a masthead that is displayed on every page you are using Url.RouteUrl to create HREF links to other pages on the site : <a href="<%= Url.RouteUrl("testimonials-route", new { }) %>" All Testimonials </a> This testimonials-route is defined in global.ascx by the first route below. Notice that the above call to RouteUrl does not provide a gender , but the route is defined with a default of 'neutral' so we'd

Pretty URL ASP.NET MVC

无人久伴 提交于 2019-11-30 04:08:22
How can I get pretty urls like localhost:8888/News/Example-post instead of localhost:8888/Home/Details/2 My HomeController has the following for the Details method public ActionResult Details(int id) { var ArticleToView = (from m in _db.ArticleSet where m.storyId == id select m).First(); return View(ArticleToView); Maxim Zaslavsky As the ASP.NET routing system is somewhat complicated, there are many ways to accomplish what you describe. First of all, do you just want to have a pretty URL for the Details method? If so, you might consider renaming HomeController to NewsController or moving the

ASP.NET MVC Areas: How to hide “Area” name in URL?

我的未来我决定 提交于 2019-11-30 03:30:46
When running the MVC 2 Areas example that has a Blog Area and Blog Controller the URL looks like this: http://localhost:50526/Blog/Blog/ShowRecent in the format: RootUrl / AreaName / ControllerName / ActionName Having just discovered MVC Areas, it seem like a great way to organise code, ie create an Area for each section, which in my case each section has its own controller. This means that each AreaName = ControllerName. The effect of this is the double AreaName/ControllerName path in the Url eg /Blog/Blog/ above Not having a complete clear understanding of routing, how could I setup routing

Defining conditional routes

邮差的信 提交于 2019-11-30 03:14:58
问题 I've been searching for something similar but no luck. I want to build an app that uses different controllers for same urls. Basic idea is like that if a user is logged in as admin he uses lets say admin controller, if user is just a user he uses user controller. This is just an example, basically I want to have a function that decides what controller route takes. Thank u everyone. Any help is greatly appreciated. PS Use of this: Admin has different UI and options, Output catching, Separation

How to find the right route in a RouteCollectionRoute?

a 夏天 提交于 2019-11-30 02:30:03
问题 I am testing ASP MVC routes. I am having an issue with attribute routes in ASP MVC 5.1 When I have a controller like this: public class FooController : Controller { [Route("foo")] [HttpGet] public ActionResult Get() { .... } [Route("foo")] [HttpPost] public ActionResult Post() { .... } } Then in order to test which route matches a particular request, I call routes.GetRouteData. I get a System.Web.Routing.RouteData that contains the route as well as values that should say which controller and

Dynamic url rewriting with MVC and ASP.Net Core

强颜欢笑 提交于 2019-11-30 02:14:22
I am re-writing my FragSwapper.com website (currently in Asp 2.0!) with ASP.Net Core and MVC 6 and I'm trying to do something I would normally have to break out a URL Re-Write tool along with db code and some Redirects but I want to know if there is a "better" way to do it in ASP.Net Core possibly with MVC Routing and Redirecting. Here are my scenarios... URL Accessing the site: [root] What to do: Go to the usual [Home] Controller and [Index] View (no [ID]). ...it does this now: app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); });

Action Parameter Naming

这一生的挚爱 提交于 2019-11-30 01:27:31
Using the default route provided, I'm forced to name my parameters "id". That's fine for a lot of my Controller Actions, but I want to use some better variable naming in certain places. Is there some sort of attribute I can use so that I can have more meaningful variable names in my action signatures? // Default Route: routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ); // Action Signature: public ActionResult ByAlias(string alias) { // Because the route specifies "id" and

How do I set the default namespaces in MapHttpRoute?

拟墨画扇 提交于 2019-11-30 01:26:05
With the standard MapRoute method a can pass a string collection representing the namespaces in which to search for my controller. This seems to have disappeared from MapHttpRoute. How does one define the default namespaces using the new API routing? Shazwazza We had this problem with the Umbraco core so we created our own IHttpControllerSelector, the source code can be found here: https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib/Selectors/NamespaceHttpControllerSelector.cs You can also install nuget package WebAPIContrib which contains

Handle Invalid URL in MVC

若如初见. 提交于 2019-11-30 00:56:40
How to handle invalid URLs in MVC? For ex.: When the user enters http://localhost/User/MyProfile instead of http://localhost/User/Profile , it will throw an exception. How to handle this request? You need first to add a custom Error page url in the web.config: <customErrors mode="On" defaultRedirect="~/Error/404" /> And add a controller to handle the invalid urls: public class ErrorController:Controller { [ActionName("404")] public ActionResult Error404() { return View("Error"); } } And if you want to redirect the user to the home page then you don't need the Error controller just modify the

No type was found that matches the controller named 'User'

╄→гoц情女王★ 提交于 2019-11-30 00:19:06
I'm trying to navigate to a page which its URL is in the following format: localhost:xxxxx/User/{id}/VerifyEmail?secretKey=xxxxxxxxxxxxxxx I've added a new route in the RouteConfig.cs file and so my RouteConfig.cs looks like this: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "VerifyEmail", url: "User/{id}/VerifyEmail", defaults: new { controller = "User", action = "VerifyEmail" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller =