asp.net 4.0+ webform程序中集成mvc4
程序员文章站
2023-01-10 15:17:22
本文为大家分享了asp.net 4.0+ webform程序中集成mvc4的方法,供大家参考,具体内容如下
1、新建packages.config文件,里面加上必要的程序...
本文为大家分享了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,即可看到效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。