Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC + WebForms - route conflict
    primarykey
    data
    text
    <p>I'm building a simple report Web app for rendering reports using ASP.NET MVC3 + WebForms. The reports themselves are rendered by the ReportViewer ASP.NET WebForms control, but I'd like use ASP.NET MVC to create the parameter entry. </p> <p>I'd like to have that all requests follow the default routing scheme of '~/{controller}/{action}/{parameters}', except requests for <code>~/Report</code>, which should go to the report rendering WebForm. What's the right way to do this?</p> <p>Expanding a bit..</p> <p>I have two routes in <code>Global.asax.cs</code> - the default one and one for the WebForms page.</p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); routes.MapPageRoute("report-rendering", "Report", "~/Render.aspx"); } </code></pre> <p>The URLs get rendered fine, but the problem with this is that when the request comes in, the first route also eats the URLs for the second one, i.e. <code>~/Report?id=7</code> tries to call the <code>Index</code> method on the <code>ReportController</code> (which doesn't exist). </p> <p>If I change it so that the 'report-rendering' route comes before the 'Default' route, like so:</p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapPageRoute("report-rendering", "Report", "~/Render.aspx"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } </code></pre> <p>Now calls to the Html.ActionLink() render incorrect URLs, i.e.</p> <pre><code>`@Html.ActionLink("Report list", "Index", "ReportList")` </code></pre> <p>Renders </p> <pre><code>`http://localhost:49910/Report?action=Index&amp;controller=ReportList` </code></pre> <p>My current workaround puts the 'Default' route first, while adding a regex constraint to ignore requests for the 'Report' controller, like so:</p> <pre><code>routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults new { controller = @"(?!report$).*" } ); </code></pre> <p>This doesn't feel clean. Again, What's the right way of doing this?</p> <p>Also, I haven't yet decided how I'll pass the parameters to the rendering form: I could use both query parameters or POST them. I'm guessing that query params are more flexible. What's the best practice here?</p> <p>EDIT: </p> <p>While researching the answer by @LeftyX, seems like I've found an answer. To quote P. Haack from his Routing chapter in the <a href="http://rads.stackoverflow.com/amzn/click/1118076583" rel="nofollow">Professional ASP.NET MVC 3</a> (Named Routes, Chapter 9, page 233):</p> <blockquote> <p>... <em>Use names for all your routes and always use the route name when generating URLs</em>. Most of the time, letting Routing sort out which route you want to use to generate a URL is really leaving it to chance, which is not something that sits well with the obsessive-compulsive control freak developer. When generating a URL, you generally know exactly which route you want to link to, so you might as well specify it by name.</p> </blockquote> <p>The mentioned section discusses a very similar situation to the one I described. </p> <p>But since <code>Html.ActionLink()</code> doesn't have an overload with the route name parameter, does this mean I cannot reliably use it anywhere in the entire app if have a route like this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload