I am adding a WebForm from which I would like to resolve routes to URLs. For example, in MVC I would just use
return RedirectToAction("Action", "Controller");
So, if you have a way of getting to that same URL from a WebForm in the same application, it would be appreciated.
Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy.
Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access. Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing an interactive web application with the latest web standards.
If you add a plain ASPX page to an ASP.NET MVC project, well, it just works like a charm without any changes to the configuration. If you invoke the ASPX page, the ASPX page is processed with viewstate and postbacks.
MVC focuses on separation of concern, i.e., there is no fixed code behind page for every view. A view can be called from multiple action. Web form based on functions and page behind code, i.e., there is code behind page for each view. You have to write code in that class related to this view only.
Try something like this in your Webform:
<% var requestContext = new System.Web.Routing.RequestContext(
new HttpContextWrapper(HttpContext.Current),
new System.Web.Routing.RouteData());
var urlHelper = new System.Web.Mvc.UrlHelper(requestContext); %>
<%= urlHelper.RouteUrl(new { controller = "Controller", action = "Action" }) %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With