1、所有应用的MVC相关程序集编译时要选择复制到本地,需要用到的程序如下图

2、IIS设置:

因为 IIS 7/8 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改。运行命令行 %windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/handlers 。
其中的 handlers 是错误信息中红字显示的节点名称。
如果modules也被锁定,可以运行%windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/modules

注意:cmd.exe要以管理员身份启动,在c:\windows\system32下找到cmd.exe,右键管理员启动,输入上面的命令即可。

3、配置文件修改,添加MVC对应配置

<?xml version="1.0" encoding="utf-8"?>

<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <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"/>
<add namespace="System.Web.Optimization"/>
<add namespace="System.Linq"/>
<add namespace="System.Collections.Generic"/>
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings> </appSettings> <system.web>
<compilation debug="true" targetFramework="4.0" />
<!--<assemblies>
<add assembly="System.Web.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>-->
<httpHandlers>
</httpHandlers>
<pages validateRequest="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Linq"/>
<add namespace="System.Collections.Generic"/>
</namespaces>
</pages> </system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" /> </system.webServer> </configuration>

3、路由设置:

 protected void Application_Start(object sender, EventArgs e)
{
RouteCollection routes = RouteTable.Routes; //避免对 Web 资源文件(例如 WebResource.axd 或 ScriptResource.axd)的请求传递给控制器
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //避免aspx页面的请求传递给控制器
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{handler}.ashx/{*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.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); //路由注册
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
}

4、自定义WebApi 带方法的路由

    routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

5、用get post ajax请求 webapi方法:

WebApi中定义的方法:

 public class ItemController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
} public string Get(int id)
{
Result res = new Result { Id = id, IsOK = true, Message = "测试成功。" };
return JsonConvert.SerializeObject(res);
//return res;
}
[HttpGet]
public string TestData(int id, string message)
{
Result res = new Result { Id = id, IsOK = true, Message = message };
return JsonConvert.SerializeObject(res);
} public string SaveData([FromBody]Result data)
{
Result res = new Result { Id = data.Id, IsOK = true, Message = data.Message };
return JsonConvert.SerializeObject(res);
//return string.Empty;
}
} public class Result
{
public int Id
{
get;
set;
} public bool IsOK
{
get;
set;
}
public string Message
{
get;
set;
}
}

AJAX调用

 $.ajax({
url: "api/item/get/1",
type: "get",
// data: { "": 1 },
success: function (data) {
alert(data);
}
});
 $.ajax({
url: "api/item/SaveData",
type: "post",
data: JSON.stringify({ id: 1, message: "测试ok?" }),
contentType: "application/json",
success: function (data) {
alert(data);
}
});

附上程序源码:http://pan.baidu.com/s/1c0e71Xe

MVC 4 与WebForm 混合应用 WebApi 发布常见问题的更多相关文章

  1. (读书笔记)Asp.net Mvc 与WebForm 混合开发

    根据项目实际需求,有时候会想在项目中实现Asp.net Mvc与Webform 混合开发,比如前台框架用MVC,后台框架用WebForm.其实要是实现也很简单,如下: (1)在MVC 中使用Webfo ...

  2. Asp.net Mvc 与WebForm 混合开发

      根据项目实际需求,有时候会想在项目中实现Asp.net Mvc与Webform 混合开发,比如前台框架用MVC,后台框架用WebForm.其实要是实现也很简单,如下: (1)在MVC 中使用Web ...

  3. [转]Asp.net Mvc 与WebForm 混合开发

    本文转自:https://www.cnblogs.com/dooom/archive/2010/10/17/1853820.html 根据项目实际需求,有时候会想在项目中实现Asp.net Mvc与W ...

  4. Taurus.MVC 2.2.3.4 :WebAPI 实现权限控制认证(及功能增强说明)

    前言: 前两天,当我还在老家收拾行旅,准备回广州,为IT连的创业再战365天时, 有网友扣上问:Taurus.MVC中如何实现认证和权限控制,最好能做个小例子. 我一不小心回了句:等回广州我再写篇文章 ...

  5. webform中使用webapi,并且使用autofac

    private void AutofacIoCRegister() { HttpConfiguration config = GlobalConfiguration.Configuration; if ...

  6. NET Core MVC 在linux上的创建及发布

    NET Core MVC 在linux上的创建及发布 前言 ASP.NET core转眼都发布半月多了,社区最近也是非常活跃,虽然最近从事python工作,但也一直对.NET念念不忘,看过了园区大神们 ...

  7. WebApi发布到外网提示404问题

    今天在做微信接口的对接,需要把webApi发布到服务器,放上去的时候,提示404 找了以后,发现了这段代码,粘贴上去就可以用了 在web.config添加如下节点 <system.webServ ...

  8. webapi发布常见错误及解决方案

    webapi发布常见错误及解决方案 错误一: 错误:404 (Not Found) 解决方案: 在  <system.webServer>节点中添加如下模块: <modules ru ...

  9. 如何将dotnet core webapi发布到docker中…

    如何将dotnet core webapi发布到docker中 今天想起来撸一下docker,中途还是遇到些问题,但是这些问题都是由于路径什么的导致不正确,在这儿还是记录下操作过程,今天是基于wind ...

随机推荐

  1. html之ol标签

    有序列表,请使用 CSS 来定义列表的类型. 通常和li配对使用 HTML5新属性: compact reversed:降序 start:有序列表的起始值 type:在列表中使用标记类型(1,A,a, ...

  2. R(五): R常用函数

    工作笔记记录,会持续更新.... 目录: apply tapply lapply sapply merge substr.substring.strsplit.unlist.paste.paste0. ...

  3. Jquery幻灯片焦点图插件

    兼容IE6/IE7/IE8/IE9,FireFox,Chrome*,Opera的 jQuery. KinSlideshow幻灯片插件,功能很多 ,基本能满足你在网页上使用幻灯片(焦点图)效果. 下载

  4. Add 4 multipath LUNs into RHEL

    1. SSH to oracle-node1 and run the following commands: # echo "- - -" > /sys/class/scsi ...

  5. android学习笔记九——RatingBar

    RatingBar==>星级评分条 RatingBar和SeekBar十分相似,它们甚至有相同的父类:AbsSeekBar.两者都允许用户通过拖动来改变进度: 两者最大的区别在于RatingBa ...

  6. C#生成二维码示例

    其实现在二维码越来越流行,网上也有很多生成二维码的类库.写一下WEB生成二维码注意事项吧! 目前C#生成二维码大部分都是使用ThoughtWorks.QRCode或者ZXing类库生成,主要说一下Th ...

  7. 151. Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  8. Linux xargs命令

    xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理.通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从 ...

  9. SpringMVC4.0.3 @ResponseBody JSON 中文乱码问题

    @RequestMapping(value="listUserJson.html",produces="text/html;charset=UTF-8") @R ...

  10. [HTMLDOM]删除已有的 HTML 元素

    摘自www.w3school.com:http://www.w3school.com.cn/htmldom/dom_elements.asp如需删除 HTML 元素,您必须清楚该元素的父元素: < ...