views:

137

answers:

4

I have to migrate a web application I made with ASP.NET MVC to a regular Visual Studio 2005 ASP.NET Web Forms based website.

I've looked at things like MonoRail, but it's too different for my co-workers (who are already uncomfortable with MVC) to use.

I've read that the first version of ASP.NET MVC was made in an airplane flight, that's around the kind of complexity I'm willing to deal with.

I don't need an ORM. I have a homegrown ORM that I've been using for a long time that I will use to replace Entity Framework.

Specifically I'm mostly looking for two things: how to do routes using a line or two in a web.config and a fancy Default.aspx, and how to render an aspx page with model data injected into it.

A: 

See these articles for guides on how to implement routing (though doing it with just one or two lines in web.config may be a lofty goal):

For injecting your model data - in the code behind retrieve the model object that you want, and use the page events (Page_Load, etc) to insert the data in to controls, or bind it to bindable controls.

Yaakov Ellis
A: 

After much Googleing I've found http://mudabone.com/?page_id=335 which pretty much looks like exactly what I want, but the source code link is broken.

wm_eddie
Finally found something that doesn't 404 http://alejandrovidalquiroga.blogspot.com/
wm_eddie
A: 

You can route as per MVC in ASP.NET.

In global.asax:

protected void Application_Start(object sender, EventArgs e)
{
    //do stuff
    RegisterRoutes(RouteTable.Routes);
    //do stuff
?

public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = true;

        routes.Add(new Route("{controller}/{action}",
           new RouteValueDictionary { { "controller", "user" }, { "action", "home" } },
           new RouteValueDictionary { { "controller", @"^(?!Resources)\w*$" }, { "action", "[a-zA-Z]+" } },// means that .htm path will go straight to the file, not thru our router
           new MvcRouteHandler()));
}

Create your own route handler

public class CustomRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        //do stuff
        string controller = RequestContext.RouteData.Values["controller"].ToString();
        string methodName =  RequestContext.RouteData.Values["action"].ToString();
        //do stuff
    }

}

public class RoutingHandler : UrlRoutingHandler
{
     protected override void VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext)
    {
    }
}

For the web.config:

<httpHandlers>
    <remove verb="*" path="*.asmx" />
    <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
    ***<add verb="*" path="UrlRouting.axd" validate="false" type="CustomHttpHandlerNamespaceAndClassName, CustomHttpHandlerNamespace" />***
</httpHandlers>
<httpModules>
    <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</httpModules>
Mike
Unfortunately the routing module is part of the .Net Framework 3.5.
wm_eddie
A: 

I was able to take the code from the blog post I mentioned before (A MVC Framework implementation in .Net 2.0 (Spanish)) and beat it into something barely usable.

Since I don't understand httpHandlers at all, I added a hack using the Intelligencia.UrlRewriter. In the end I added the following to my web.config

    <httpModules>
  <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
 <httpHandlers>
  <add verb="*" path="*.mvc"   type="MyApp.MainController"/>
  <remove verb="*" path="/MyApp/Views/*"/>
  <remove verb="*" path="/MyApp/Views/*/*"/>
  <remove verb="*" path="/MyApp/Content/*"/>
  <remove verb="*" path="/MyApp/Content/*/*"/>
  <remove verb="*" path="/MyApp/Scripts/*"/>
  <remove verb="*" path="/MyApp/Scripts/*/*"/>
 </httpHandlers>
</system.web>

By doing this I had to append a aspx to the end of the url like so: /MyApp/Home/Index.aspx. I tried using a .mvc extention, but that didn't work either.

I still have problems with Posting though. It'll take a fair amount of work to get Alejandro's MainController to handle multiple post variables properly, I'm probably going to have to make a FormCollection class.

wm_eddie