asp.net-mvc-routing

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

浪尽此生 提交于 2019-12-18 11:41:48
问题 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

Handle Invalid URL in MVC

折月煮酒 提交于 2019-12-18 10:57:10
问题 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? 回答1: 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

ASP.NET MVC 5 (Visual Studio 2013 Preview) Change Login Url for [Authorize]

こ雲淡風輕ζ 提交于 2019-12-18 09:08:21
问题 Hey guys I started playing around with the ASP.NET MVC 5 preview and everything has been going fine so far (I can only recommend it). However, I wonder where I can set the Login-Url for the Built-In [Authorize] -Attribute. I've moved the AccountController to a an area, so the path to the Login action is no longer /Account/Login but MyArea/Account/Login , which is ignored by the [Authorize] -Attribute, which in turn means, that whenever one navigates to a controller or action with the

How do I enable special characters in MVC routing?

痞子三分冷 提交于 2019-12-18 08:01:53
问题 I'm using asp.net MVC 4. These are my routes: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}" ); My current controller responds to the following request correctly: http://localhost:2020/PrivacyUrls/Details/ct14524 How can I validate urls like these? http://localhost:2020/PrivacyUrls/Details/*ct14524 http://localhost:2020/PrivacyUrls/Details/&ct14524 which now returns 404. A potentially dangerous Request.Path value was detected from the client (*). A potentially dangerous

How do I enable special characters in MVC routing?

♀尐吖头ヾ 提交于 2019-12-18 08:01:51
问题 I'm using asp.net MVC 4. These are my routes: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}" ); My current controller responds to the following request correctly: http://localhost:2020/PrivacyUrls/Details/ct14524 How can I validate urls like these? http://localhost:2020/PrivacyUrls/Details/*ct14524 http://localhost:2020/PrivacyUrls/Details/&ct14524 which now returns 404. A potentially dangerous Request.Path value was detected from the client (*). A potentially dangerous

ASP.NET MVC - get list of objects from query string

一笑奈何 提交于 2019-12-18 06:54:05
问题 I'm passed a list of parameters. Such as "Name", "Id", "Type". There will be an many of these in url, like so: "Name=blah1,Id=231,Type=blah1;Name=blah2,Id=2221,Type=blah1;Name=blah3,Id=45411,Type=blah3;" I wonder if there is a way to map these query parameters to a List of objects. So, I can create an object: MyTestObject {Name;Id;Type} and can say in my controller Index(IList<MyTestObject> params) params will be filled in with data from query string. Something that is similar to http:/

Possible Bug With ASP.NET MVC 3 Routing?

无人久伴 提交于 2019-12-18 04:35:20
问题 Normally i wouldn't put a title like this in the question, but i'm pretty sure it's a bug (or by design?) I created a brand new ASP.NET MVC 3 Web Application. Then i went to the /Home/About page. The URL for this page is: http://localhost:51419/Home/About Then i changed the URL to this: http://localhost:51419/(A(a))/Home/About And the page worked? Looking at the route values, controller = Home, Action = About. It's ignored the first part? And if i look at all the links in the source: <link

Test if a request's URL is in the Route table

烈酒焚心 提交于 2019-12-18 04:17:09
问题 I want to test if a URL is part of the routes defined in the Global.asax . This is what I have: var TheRequest = HttpContext.Current.Request.Url.AbsolutePath.ToString(); var TheRoutes = System.Web.Routing.RouteTable.Routes; foreach (var TheRoute in TheRoutes) { if (TheRequest == TheRoute.Url) //problem here { RequestIsInRoutes = true; } } The problem is that I can’t extract the URL from the route. What do I need to change? 回答1: The problem is that I can't extract the URL from the route. I

setting query string in redirecttoaction in asp.net mvc

三世轮回 提交于 2019-12-18 04:16:20
问题 I have to do a redirecttoaction call in asp.net mvc view with varying params, extracted from the referrer page of the view (the status of a grid). I have (in an hidden field) the content of the query string (sometimes empty, sometimes with 2 parameters and so on), so I have problems to create the route values array. Are there some helpers, that help me to convert a query string a route values array? Something like: string querystring ="sortdir=asc&pag=5"; return RedirectToAction( "Index",

The view 'Index' or its master was not found.

跟風遠走 提交于 2019-12-18 03:00:45
问题 The view 'Index' or its master was not found. The following locations were searched: ~/Views/ControllerName/Index.aspx ~/Views/ControllerName/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx I got this error when using ASP.Net mvc area. The area controller action are invoked, but it seems to look for the view in the 'base' project views instead of in the area views folder. 回答1: What you need to do is set a token to your area name: for instance: context.MapRoute( "SomeArea