Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access HtmlHelpers from WebForm when using ASP.NET MVC

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.

like image 407
oglester Avatar asked Aug 13 '09 20:08

oglester


People also ask

Can I use webform in MVC?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy.

When should you use .NET Web Forms Over ASP.NET MVC?

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.

Can you use ASPX pages in MVC?

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.

What is difference between webform and MVC?

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.


1 Answers

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" }) %>
like image 125
eu-ge-ne Avatar answered Sep 20 '22 12:09

eu-ge-ne