0

So I'm writing an ASP.NET MVC app and I have a little bit problem with routing aspx file - in general making this work.
Let's say I have a razor page and I want to, for example open specific row from database and show it, it's very simple and I just write in index.cshtml:

@Url.Action("Details", new { id = item.DB_Id })

And details page opens and I can see specific informations of this row in database
Code of routing:

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", 
                            action = "Index", 
                            id = UrlParameter.Optional 
                          }

But when I want do the same but instead of opening details.cshtml file I'd like to do it with details.aspx (Web Form) appears a problem. Is controller has to be different, is code of routing has to be different? Or is it basically possible? And ideas or hints?

Rajput
  • 2,597
  • 16
  • 29
imaco
  • 27
  • 1
  • 7
  • see if [this answer](http://stackoverflow.com/questions/10175200/how-to-route-a-aspx-page-in-asp-net-mvc-3-project) or [this](http://stackoverflow.com/questions/10590662/route-from-incoming-aspx-url-to-an-asp-net-mvc-controller-action) helps you – Elmer Dantas Dec 29 '16 at 15:01
  • @ElmerDantas I can't even open my .aspx file with parametr from .cshtml. Is it even possible? Is it only doable .aspx -> .aspx? And how has to .aspx file look like to get that parametr and go to specific database? – imaco Dec 29 '16 at 15:50
  • Another [link](http://stackoverflow.com/questions/7407801/mixing-razor-views-and-web-forms-master-pages) that maybe helps you. I've never need to do such a think...so I don't know if its possible. I was curious and start to read to see if it's possible to achieve it – Elmer Dantas Dec 29 '16 at 15:58
  • @ElmerDantas if I may ask - what is your personal preference? .cshtml MVC or WebForms .aspx? – imaco Dec 29 '16 at 16:08
  • definitely .cshtml. I used to work with .aspx but when I got the chance, I've changed do .cshtml MVC. webForms .aspx is too messy related to MCV .cshtml...in my opinion. – Elmer Dantas Dec 29 '16 at 16:21

1 Answers1

0

Url.Action helper doesn't create URL's to Web Forms pages because they aren't Actions. You'll need to do something like

@Url.Content("~/somefolder/Details.aspx?id=" + item.DB_Id)

Url.Content is meant for creating URL's to static files, but it also works well with Web Forms.

You could create your own helper that handles parameters more cleanly. I don't have time to do the implementation right now, but you could create something like:

@Url.WebFormsPage("~/somefolder/Details.aspx", new { id = item.DB_Id })

The helper could use reflection to generate the appropriate query string and append it to the URL.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Thank you my man! I spent 2 days trying to make it work by changing routes, controllers etc. Now I've got what I want! – imaco Dec 29 '16 at 17:06
  • @imaco It may be possible to use a built in helper, assuming you register your Web Forms page routes. I'm not sure, I've never tried it. See [UrlHelper.RouteUrl](https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.routeurl(v=vs.118).aspx#M:System.Web.Mvc.UrlHelper.RouteUrl%28System.String,System.Object%29) and [how to define routes in Web Forms](https://msdn.microsoft.com/en-us/library/cc668177.aspx). If I were you I'd prefer to just go all MVC and ditch Web Forms, but it might be useful if you already have Web Forms pages that do what you want. – mason Dec 29 '16 at 17:11