ASP.NET MVC 与 ASP.NET Web API 异同 区别 路由

ASP.NET MVC 筛选器  
Web API 示例列表 
文档目录  
.NET 文档  
ASP.NET 文档  
ASP.NET Web API Routing 路由
ASP.NET Web API 2 的操作结果
ASP.NET Web API中的参数绑定 FromUri,FromBody

1、Controller 区别
ASP.NET MVC:继承 System.Web.Mvc.Controller
ASP.NET Web API:继承 System.Web.Http.ApiController
*
2、方法返回类型 区别
ASP.NET MVC 控制器操作方法返回类型如下:
ActionResult:

ASP.NET Web API 控制器操作方法返回类型如下:
void:返回空 204 (无内容)。
HttpResponseMessage:直接转换为 HTTP 响应消息。
IHttpActionResult:调用 ExecuteAsync 以创建 HttpResponseMessage,然后转换为 HTTP 响应消息。
其他类型的:将序列化的返回值写入响应正文;返回 200 (OK) 。

3、文件下载
ASP.NET MVC return File

return File(buffer, "image/jpeg");

return File(buffer, "text/plain", "PaymentInvoice.csv");

return File(buffer, "application/vnd.ms-excel", $"{DateTime.Now.ToString("yyyyMMddhhmmss")}.xlsx");

ASP.NET Web API HttpResponseMessage

[Route("api/hospital/export")]
[HttpGet]
public HttpResponseMessage Export()
{
    List<sys_area> list = new List<sys_area>();
    list.Add(new sys_area { code_id = "110000", name = "北京市" });
    list.Add(new sys_area { code_id = "130000", name = "河北省" });

    #region excel
    var runNum = 0;
    var workbook = new XSSFWorkbook();
    var worksheet = workbook.CreateSheet("Sheet1");
    var workRowHead = worksheet.CreateRow(runNum);

    workRowHead.CreateCell(0).SetCellValue("编号");
    workRowHead.CreateCell(1).SetCellValue("省");

    runNum++;

    foreach (var item in list)
    {
        var workRow = worksheet.CreateRow(runNum);
        workRow.CreateCell(0).SetCellValue(item.code_id);
        workRow.CreateCell(1).SetCellValue(item.name);
        runNum++;
    }

    using (MemoryStream memoryStream = new MemoryStream())
    {
        workbook.Write(memoryStream);

        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(memoryStream.ToArray())
        };
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
        {
            FileName = $"地区{DateTime.Now.ToString("yyyyMMddhhmmss")}.xlsx"
        };
        return result;
    }
    #endregion
}

3、全局过滤器
ASP.NET MVC:FilterConfig.cs
ASP.NET Web API:WebApiConfig.cs
{
{
{
{

4、拦截
MVC       由 System.Web.Mvc.ActionFilterAttribute 来做action请求的拦截。
Web API 由 System.Web.Http.Filters.ActionFilterAttribute 来处理。
*
5、Authorize 授权
System.Web.Mvc.AuthorizeAttribute 用于Web MVC(视图控制器)
System.Web.Http.AuthorizeAttribute 用于Web API
*
System.Web.Http.Controllers.HttpActionContext 用于Web API
*
6、ApiController 常用
System.Web.Http.HttpRequestContext RequestContext { get; set; }
System.Web.Http.HttpRequestMessage Request { get; set; }
System.Web.Http.ModelStateDictionary ModelState { get; }
System.Web.Http.HttpActionContext ActionContext { get; set; }
System.Web.Http.HttpControllerContext ControllerContext { get; set; }

7、web api 使用 request form
web api 使用

HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
HttpRequestBase request = context.Request;//定义传统request对象
string a = request.Form["Key"];

*
*

var token = HttpContext.Current.Request.Headers["token"];
var uid = HttpContext.Current.Request.Form["uid"];
var aa = HttpContext.Current.Request.Form["list[aa]"];

*
*
*
*
*
*
*
8、
9、
0、

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值