添加dto模式的demo测试
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.IO;
|
||||
using Yi.Framework.DTOModel.RABC.Student.MapperConfig;
|
||||
using Yi.Framework.WebCore.Mapper;
|
||||
|
||||
namespace Yi.Framework.WebCore.AspNetCoreExtensions
|
||||
@@ -13,7 +14,9 @@ namespace Yi.Framework.WebCore.AspNetCoreExtensions
|
||||
{
|
||||
public static IServiceCollection AddAutoMapperService(this IServiceCollection services)
|
||||
{
|
||||
services.AddAutoMapper(typeof(AutoMapperProfile));
|
||||
//这里会通过反射自动注入的,先临时这样
|
||||
services.AddAutoMapper(typeof(AutoMapperProfile),typeof(StudentProfile));
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
@@ -6,7 +6,7 @@ using Yi.Framework.Common.Const;
|
||||
using Yi.Framework.Common.Enum;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Core.Cache;
|
||||
using Yi.Framework.DTOModel;
|
||||
using Yi.Framework.DTOModel.Base.Dto;
|
||||
using Yi.Framework.Model.RABC.Entitys;
|
||||
using Yi.Framework.WebCore.CommonExtend;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.DTOModel.Vo;
|
||||
using Yi.Framework.DTOModel.Base.Vo;
|
||||
using Yi.Framework.Model.BBS.Entitys;
|
||||
using Yi.Framework.Model.RABC.Entitys;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using log4net.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
@@ -7,6 +8,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Enum;
|
||||
using Yi.Framework.Common.Exceptions;
|
||||
using Yi.Framework.Common.Models;
|
||||
|
||||
namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
@@ -17,11 +19,12 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
public class ErrorHandExtension
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
private ILogger<ErrorHandExtension> _logger;
|
||||
public ErrorHandExtension(RequestDelegate next,ILogger<ErrorHandExtension> logger)
|
||||
//private readonly IErrorHandle _errorHandle;
|
||||
public ErrorHandExtension(RequestDelegate next/*, IErrorHandle errorHandle*/)
|
||||
{
|
||||
this.next = next;
|
||||
_logger = logger;
|
||||
//this._logger = loggerFactory.CreateLogger<ErrorHandExtension>();
|
||||
//_errorHandle = errorHandle;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
@@ -30,14 +33,19 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
{
|
||||
await next(context);
|
||||
}
|
||||
catch (BusinessException businessEx)
|
||||
{
|
||||
var statusCode = 200;
|
||||
await HandleExceptionAsync(context, statusCode, businessEx.Message, businessEx.Code);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//await _errorHandle.Invoer(context, ex);
|
||||
var statusCode = context.Response.StatusCode;
|
||||
if (ex is ArgumentException)
|
||||
{
|
||||
statusCode = 200;
|
||||
}
|
||||
_logger.LogError($"中间件抓取错误\r\n错误信息:{ex.Message}\r\n堆栈信息“{ex.StackTrace}");
|
||||
await HandleExceptionAsync(context, statusCode, ex.Message);
|
||||
}
|
||||
finally
|
||||
@@ -45,9 +53,9 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
var statusCode = context.Response.StatusCode;
|
||||
var msg = "";
|
||||
|
||||
switch (statusCode)
|
||||
switch (statusCode)
|
||||
{
|
||||
case 401: msg = "未授权";break;
|
||||
case 401: msg = "未授权"; break;
|
||||
case 403: msg = "未授权"; break;
|
||||
case 404: msg = "未找到服务"; break;
|
||||
case 502: msg = "请求错误"; break;
|
||||
@@ -59,22 +67,20 @@ namespace Yi.Framework.WebCore.MiddlewareExtend
|
||||
}
|
||||
}
|
||||
//异常错误信息捕获,将错误信息用Json方式返回
|
||||
private static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg)
|
||||
private static Task HandleExceptionAsync(HttpContext context, int statusCode, string msg, ResultCodeEnum code = ResultCodeEnum.NotSuccess)
|
||||
{
|
||||
Result resp;
|
||||
Result result;
|
||||
if (statusCode == 401)
|
||||
{
|
||||
resp = Result.UnAuthorize(msg);
|
||||
result = Result.UnAuthorize(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
resp = Result.Error(msg);
|
||||
result = Result.Error(msg).SetCode(code);
|
||||
}
|
||||
resp.SetCode((ResultCodeEnum)statusCode);
|
||||
var result = JsonConvert.SerializeObject(resp);
|
||||
|
||||
context.Response.ContentType = "application/json;charset=utf-8";
|
||||
|
||||
return context.Response.WriteAsync(result);
|
||||
return context.Response.WriteAsync(JsonConvert.SerializeObject(result));
|
||||
}
|
||||
}
|
||||
//扩展方法
|
||||
|
||||
Reference in New Issue
Block a user