2009-11-04 67 views
1

我必須將我用ASP.NET MVC製作的Web應用程序遷移到基於Visual Studio 2005 ASP.NET Web Forms的常規網站。ASP.NET 2.0的最小MVC設置

我已經看過像MonoRail這樣的東西,但是對於我的同事(他們已經對MVC感到不舒服)的使用太不同了。

我讀過第一個版本的ASP.NET MVC是在飛機飛行中完成的,那是我願意處理的那種複雜性。

我不需要ORM。我有一個自己開發的ORM,我一直用它來替換實體框架。

具體而言,我主要是在尋找兩件事:如何使用web.config中的一行或兩行和花哨的Default.aspx來完成路由,以及如何渲染注入模型數據的aspx頁面。

回答

0

我能夠從取碼博客文章我之前提到過(A MVC Framework implementation in .Net 2.0 (Spanish)),並將其打成幾乎不可用的東西。

因爲我根本不理解httpHandlers,所以我使用Intelligencia.UrlRewriter添加了一個hack。最後,我添加了以下到我的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> 

通過這樣做,我不得不一個ASPX追加到像這樣的url結束:/ MyApp的/首頁/索引的.aspx。我嘗試使用.mvc擴展,但是這也不起作用。

雖然我仍然有問題。它需要大量的工作才能讓Alejandro的MainController正確處理多個post變量,我可能不得不製作一個FormCollection類。

0

請參閱有關如何實現路徑導引這些文章(雖然在web.config中只有一個或兩行這樣做可能是一個崇高的目標):

注入模型的數據 - 在後面的代碼檢索所需的模型對象,並使用頁面事件(的Page_Load等)插入到控制數據,或將其綁定到可綁定的控件。

0

經過多次Googleing之後,我發現http://mudabone.com/?page_id=335幾乎看起來像我想要的,但源代碼鏈接已損壞。

+0

最後發現一些不404錯誤http://alejandrovidalquiroga.blogspot.com/ – 2009-11-04 10:57:44

1

您可以根據ASP.NET中的MVC進行路由。

在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())); 
} 

創建自己的路由處理

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) 
    { 
    } 
} 

對於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> 
+0

不幸的是,路由模塊是.Net Framework 3.5的一部分。 – 2009-11-05 00:37:51