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、