Integrated mvc4_ practical skills in asp.net 4.0+ webform programs

Source: Internet
Author: User

This article for everyone to share the ASP.net 4.0+ webform program Integration Mvc4 method, for everyone's reference, the specific contents are as follows

1, the new Packages.config file, including the necessary assembly

<?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"/> & Lt;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, restore the package in the corresponding Web project

Update-package-projectname ' web '-reinstall

3, the new App_start directory, plus MVC corresponding configuration code

BundleConfig.cs the configuration code for the static file compression, the reference code is as follows:

 public class Bundleconfig
 {
  //bundling for more information, please visit 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 for routing configuration Code, Web form related resources to ignore routing filtering here

 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 for WEBAPI routing configuration, reference code:

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. Add the following code to the Application_Start event in the global file to enable the program to start the MVC configuration in effect

 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, create a new Controllers folder, add controller class inside, for example

 public class Docreccontroller:controller
 {public
  actionresult Index ()
  {
   Viewbag.username = " Wilson.fu "; return View ();
  }
}

6, the new Views folder, which plus the corresponding view file, if you need to use the template, you also need to add _viewstart.cshtml files, such as docrec/index.cshtml, documents as follows

@{
 Layout = null;
}

<! DOCTYPE html>

 
 

The Views folder also needs to be accompanied by a Web.config file for request filtering

<?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>  

The directory structure is as follows:

After compiling, you can see the effect by accessing the/docrec/index:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.