asp.net 4.0+ webform程式中整合mvc4_實用技巧

來源:互聯網
上載者:User

本文為大家分享了asp.net 4.0+ webform程式中整合mvc4的方法,供大家參考,具體內容如下

1、建立packages.config檔案,裡面加上必要的程式集 

<?xml version="1.0" encoding="utf-8"?><packages> <package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.0" targetFramework="net40" /> <package id="Microsoft.AspNet.Mvc.zh-Hans" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.Razor" version="2.0.20715.0" targetFramework="net40" /> <package id="Microsoft.AspNet.Razor.zh-Hans" version="2.0.20715.0" targetFramework="net40" /> <package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net40" /> <package id="Microsoft.AspNet.Web.Optimization.zh-Hans" version="1.0.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebApi.Client.zh-Hans" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebApi.Core.zh-Hans" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebApi.WebHost.zh-Hans" version="4.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net40" /> <package id="Microsoft.AspNet.WebPages.zh-Hans" version="2.0.20710.0" targetFramework="net40" /> <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" /> <package id="Microsoft.Net.Http.zh-Hans" version="2.0.20710.0" targetFramework="net40" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" /> <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net40" /> <package id="WebGrease" version="1.1.0" targetFramework="net40" /></packages>

2、在對應web項目中還原包 

update-package -projectname 'web' -reinstall

3、建立App_Start目錄,在裡面加上mvc對應配置代碼 

BundleConfig.cs為靜態檔案壓縮的配置代碼,參考代碼如下: 

 public class BundleConfig {  // 有關 Bundling 的詳細資料,請訪問 http://go.microsoft.com/fwlink/?LinkId=254725  public static void RegisterBundles(BundleCollection bundles)  {   bundles.Add(new ScriptBundle("~/bundles/jquery").Include(      "~/Scripts/jquery-{version}.js"));   bundles.Add(new ScriptBundle("~/bundles/common").Include("~/js/common*"));   bundles.Add(new ScriptBundle("~/bundles/echarts").Include("~/js/echarts.common*"));   bundles.Add(new ScriptBundle("~/bundles/mustache").Include("~/js/mustache*"));   bundles.Add(new ScriptBundle("~/bundles/blockUI").Include("~/js/jquery.blockUI*"));   bundles.Add(new StyleBundle("~/Content/oa/css").Include("~/css/oa/style.css"));   //BundleTable.EnableOptimizations = true;  } }

RouteConfig.cs為路由配置代碼,web form相關資源要在此處忽略路由過濾 

 public class RouteConfig {  public static void RegisterRoutes(RouteCollection routes)  {   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");   //routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");   //routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");   routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");   routes.IgnoreRoute("{handler}.ashx/{*pathInfo}");   routes.IgnoreRoute("Handlers/{handler}.aspx/{*pathInfo}");   routes.IgnoreRoute("ajaxpro/prototype.ashx");   routes.IgnoreRoute("ajaxpro/core.ashx");   routes.IgnoreRoute("ajaxpro/converter.ashx");   routes.IgnoreRoute("ajaxpro/{resource}.ashx");   routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");   routes.MapRoute(    name: "Default",    url: "{controller}/{action}/{id}",    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }   );  } }

WebApiConfig.cs為WebApi的路由配置,參考代碼: 

public static class WebApiConfig {  public static void Register(HttpConfiguration config)  {   config.Routes.MapHttpRoute(    name: "DefaultApi",    routeTemplate: "api/{controller}/{id}",    defaults: new { id = RouteParameter.Optional }   );  } }

4、Global檔案中Application_Start事件中加上如下代碼,使程式啟動MVC配置生效 

 AreaRegistration.RegisterAllAreas();   GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/json"));   WebApiConfig.Register(GlobalConfiguration.Configuration);   FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);   RouteConfig.RegisterRoutes(RouteTable.Routes);   BundleConfig.RegisterBundles(BundleTable.Bundles);   GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); 

5、建立Controllers檔案夾,在裡面加上控制器類,例如 

 public class DocRecController : Controller {  public ActionResult Index()  {   ViewBag.UserName = "wilson.fu";return View();  }}

6、建立Views檔案夾,裡面加上對應視圖檔案,如果需要使用模板,還需增加_ViewStart.cshtml 檔案,例如DocRec/Index.cshtml,檔案如下 

@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title></title></head><body> <div> <h3>  @ViewBag.UserName </h3> </div></body></html>

Views檔案夾下還需要加上Web.config檔案進行請求過濾 

<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> </sectionGroup> </configSections> <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage">  <namespaces>  <add namespace="System.Web.Mvc" />  <add namespace="System.Web.Mvc.Ajax" />  <add namespace="System.Web.Mvc.Html" />  <add namespace="System.Web.Routing" />  </namespaces> </pages> </system.web.webPages.razor> <appSettings> <add key="webpages:Enabled" value="false" /> </appSettings> <system.web> <httpHandlers>  <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/> </httpHandlers> <!--  在視圖頁面中啟用請求驗證將導致驗證在  控制器已對輸入進行處理後發生。預設情況下,  MVC 在控制器處理輸入前執行請求驗證。  若要更改此行為,請對控制器或操作  應用 ValidateInputAttribute。 --> <pages  validateRequest="false"  pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"  userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">  <controls>  <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />  </controls> </pages> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <handlers>  <remove name="BlockViewHandler"/>  <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> </handlers> </system.webServer></configuration>

目錄結構如下:

 

編譯通過後,訪問/docrec/index,即可看到效果:

 

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.