asp.net mvc routing - Linking to an MVC Route from Web API inside an in-memory server

I'm having problems testing the generation of MVC Routes from inside Web API. The code works when hit manually, but fails under test as the in-memory instance of Web API is unaware of the MVC routes and I can't figure out how to add them.Here's an example project on github illustrating the problem, but I'll include some relevant code here.I'm using an in-memory HTTP Server to host the Web API for integration testing: private HttpConfiguration _config; private HttpServer _server; private HttpMessageInvoker _client; [TestInitialize] ...Read more

.NET Core 2.0 Web API Controller routing (POST with optional [FromBody] Parameter) for receiving web hooks

Currently I try to implement a .NET Core 2.0 Web API web hook.The webhook on the sending server can simply be configured as:"endpoint": "http://localhost:50100/api/Hook"The sending server does two things:on startup it sends an empty post to the defined endpoint to check if it is there and which media types are supportedlater on the events are sent as plain json in the post body to the defined endpointI implemented a HookController and know I am facing a (for me) strange routing problem.The controller implementation is as follows: [Route("api...Read more

asp.net mvc routing - Force route placeholder to be always treated as query string parameter

I have the following route on an action within an controller that has a route prefix:[Route("{page?}", Name = "MyRoute")]public async Task<ActionResult> Index(int page = 1)IUrlHelper.RouteUrl gives me an url ending with /1 for MyRoute and Page = 1 as route values. This isn't wrong but I'd like page to always be a query string parameter.How can I do this in MVC 6? If possible I want to avoid parsing the result of RouteUrl method and setting the page as query string parameter myself. Routing docs are still empty. This answer suggests using ...Read more

asp.net mvc routing - Enforce Hyphens in .NET MVC 4.0 URL Structure

I'm looking specifically for a way to automatically hyphenate CamelCase actions and views. That is, I'm hoping I don't have to actually rename my views or add decorators to every ActionResult in the site.So far, I've been using routes.MapRouteLowercase, as shown here. That works pretty well for the lowercase aspect of URL structure, but not hyphens. So I recently started playing with Canonicalize (install via NuGet), but it also doesn't have anything for hyphens yet.I was trying...routes.Canonicalize().NoWww().Pattern("([a-z0-9])([A-Z])", "$1-$...Read more

asp.net core mvc - MVC6 View Referencing issue due to routing

In my solution I have at the root level a Controller, Views Folder each of these have a Home Folder with an Home Controller with Index method and the view folder with an index view (default MVC setup). At the root level I have introduced an Areas folder then in here I have created another Folder for the area then folders for controllers and views. Home controller with index method and views folder with index view. it all builds fine, but I receive the following error: AmbiguousActionException: Multiple actions matched. The following actions ma...Read more

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

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 / ActionNameHaving 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/ aboveNot having a complete clear unde...Read more

Routing error with default url in ASP.NET MVC 6

I have a routing problem in an MVC 6 web application : when I set route parameter in the controller used by default, application send a 404 error.My routing configuration : app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Dashboard}/{action=Index}/{id?}"); });My dashboard controller (application works) :[Authorize]public class DashboardController : Controller{ public DashboardController() { } [HttpGet] public IActionResult Index() =&...Read more

How to define an optional parameter with attribute routing on any level of url?

I am again here with my foolish MVC routing questions.I am creating an E commerce project and stuck at one point in routing. So far attribute routing working fine with following URL.abc.com/Electronics/Audio/Portable Audio/iPodsin form ofCategory/Subcategory/Subsubcategory/Types/productname{stuck here}But now I need to show the product name and here I am stuck. Product can be at any level. Likeabc.com/Electronics/Apple iPod Nanoabc.com/Electronics/Audio/Apple iPod Nanoabc.com/Electronics/Audio/Portable Audio/Apple iPod Nanoabc.com/Electronics/A...Read more

rewrite - ASP.NET MVC 2 and custom httpModule precedence

I have a custom HttpModule rewrite engine in an existing web application project that maps urls of the form/tom/dick/harry/.../.../...to a hierarchical navigation system stored in a database, ultimately resulting in a HttpContext.Current.RewritePath() call to the .aspx page that the requested path resolves to.I'm interested in mixing MVC in with the existing app. If the MVC code works well and provides real benefit over the code behind model, how will I handle precedence between my rewrite engine and the routetable? Is the routetable referenced...Read more

asp.net mvc routing - How to prevent Url.RouteUrl(...) from inheriting route values from the current request

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 ge...Read more

Attribute Routing in ASP.NET Core 1.0

Do I need to configure anything to use attribute routing in an ASP.NET Core 1.0 application?The following doesn't seem to be working for me. I was expecting to hit this method when I go to localhost:132/accounts/welcomepublic class AccountsController : Controller{ [Route("welcome")] public IActionResult DoSomething() { return View(); }}...Read more

asp.net mvc routing - Route attributes overriding route

Here's my route: routes.MapRoute("Login", "", new { action = "Login", controller = "Authentication"}) .DataTokens = new RouteValueDictionary(new { area = "Authentication" }); routes.MapMvcAttributeRoutes();Here is the controller with action:[RouteArea("Authentication", AreaPrefix = "auth")][Route("{action=Login}")]public class AuthenticationController : BaseController{ [HttpGet] [AllowAnonymous] public ActionResult Login() {...If I comment out routes.MapMvcAttributeRoutes(); then I can request the Login acti...Read more

asp.net mvc routing - Hiding Get-Action in Web API (mvc)

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 Controller:public class UserController { public HttpResponseMessage Get(int id) {} public HttpRespnseMesseage GetDetails(int id) ...Read more