Integration of mvc4 and webformmvc4 in the asp.net 4.0 + webform Program

Source: Internet
Author: User
Tags webhost

Integration of mvc4 and webformmvc4 in the asp.net 4.0 + webform Program

This article shares with you the mvc4 Integration Method in the asp.net 4.0 + webform program for your reference. The specific content is as follows:

1. Create the packages. config file and add 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" /> <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. Create the App_Start directory and add the mvc configuration code to it.

BundleConfig. cs is the configuration code for static File compression. The reference code is as follows:

Public class BundleConfig {// For more information about bunddling, visit the 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 is the routing configuration code, and web form-related resources must ignore route 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 is the routing configuration of WebApi. Refer to the 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 MVC configuration of the program to take 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 and add the Controller class to it, for example

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

6. Create a New View folder with the corresponding View File. If you need to use the template, add the _ ViewStart. cshtml file, such as DocRec/Index. cshtml. The file is as follows:

@{ Layout = null;}<!DOCTYPE html>

Add the Web. config file to the Views folder to filter requests.

<? 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 = 31 BF3856AD364E35 "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 compilation, access/docrec/index to see the effect:

 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.