How can I create a friendly URL in ASP.NET MVC?

There are two steps to solve this problem. First, create a new route or change the default route to accept an additional parameter: routes.MapRoute( “Default”, // Route name “{controller}/{action}/{id}/{ignoreThisBit}”, new { controller = “Home”, action = “Index”, id = “”, ignoreThisBit = “”} // Parameter defaults ) Now you can type whatever you want to … Read more

How can I create a route constraint of type System.Guid?

Create a RouteConstraint like the following: public class GuidConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if (values.ContainsKey(parameterName)) { string stringValue = values[parameterName] as string; if (!string.IsNullOrEmpty(stringValue)) { Guid guidValue; return Guid.TryParse(stringValue, out guidValue) && (guidValue != Guid.Empty); } } return false; }} Next when adding … Read more

How to access the current HttpRequestMessage object globally?

It’s not impossible as I have just recently found out. It’s actually added into the Items property of the current HttpContext (if there is one) =[ HttpRequestMessage httpRequestMessage = HttpContext.Current.Items[“MS_HttpRequestMessage”] as HttpRequestMessage Edit: This is as of WebAPI v2 .. I cannot be sure of previous versions.

How am I supposed to use ReturnUrl = ViewBag.ReturnUrl in MVC 4

When using forms authentication and the user is not authenticated or authorized the ASP.NET security pipeline will redirect to the login page and pass as a parameter in the query string the returnUrl equal to the page that redirected to the login page. The login action grabs the value of this parameter and puts it … Read more

ASP.NET CORE, Web API: No route matches the supplied values

ASP.net core 3 Why this problem occurs: As part of addressing dotnet/aspnetcore#4849, ASP.NET Core MVC trims the suffix Async from action names by default. Starting with ASP.NET Core 3.0, this change affects both routing and link generation. See more: ASP.NET Core 3.0-3.1 | Breaking Changes | Async suffix trimmed from controller action names As @Chris … Read more

How to correctly use Html.ActionLink with ASP.NET MVC 4 Areas

I hate answering my own question, but @Matt Bodily put me on the right track. The @Html.Action method actually invokes a controller and renders the view, so that wouldn’t work to create a snippet of HTML in my case, as this was causing a recursive function call resulting in a StackOverflowException. The @Url.Action(action, controller, { … Read more

Attribute Routing not working in areas

You are probably combining convention based routing with attribute routing, and you should register your areas after you map the attribute routes. The line AreaRegistration.RegisterAllAreas(); should be called AFTER this line: routes.MapMvcAttributeRoutes(); The explanation (from https://devblogs.microsoft.com/aspnet/attribute-routing-in-asp-net-mvc-5/): If you are using both Areas with route attributes, and areas with convention based routes (set by an AreaRegistration … Read more