This commit is contained in:
@@ -27,7 +27,7 @@ namespace Ewide.Core.WebApi
|
||||
catch (HttpResponseException ex)
|
||||
{
|
||||
var code = ex.Response.StatusCode;
|
||||
var result = BaseDisplayJSON.Display(code, "请求方式错误");
|
||||
var result = BaseDisplayJSON.Display(code, code == HttpStatusCode.NotFound ? "找不到接口,请确认接口地址是否正确" : "请求方式错误");
|
||||
if (code == HttpStatusCode.NotFound || code == HttpStatusCode.MethodNotAllowed)
|
||||
{
|
||||
ex.Response.Content = new ObjectContent(result.GetType(), result, formatter);
|
||||
|
||||
@@ -16,26 +16,21 @@ namespace Ewide.Core.WebApi
|
||||
// Web API 路由
|
||||
config.MapHttpAttributeRoutes();
|
||||
|
||||
#region 处理接口报错
|
||||
// 404
|
||||
config.Services.Replace(typeof(IHttpControllerSelector), new HttpNotFoundDefaultHttpControllerSelector(config));
|
||||
// 500
|
||||
config.Services.Replace(typeof(IHttpActionInvoker), new HttpWebApiControllerActionInvoker(config));
|
||||
// 405
|
||||
config.Services.Replace(typeof(IHttpActionSelector), new HttpNotFoundControllerActionSelector(config));
|
||||
#endregion
|
||||
|
||||
config.Routes.MapHttpRoute(
|
||||
name: "DefaultApi",
|
||||
routeTemplate: "api/{controller}/{id}",
|
||||
defaults: new { id = RouteParameter.Optional }
|
||||
);
|
||||
|
||||
config.Routes.MapHttpRoute(
|
||||
name: "DefaultAreaApi",
|
||||
routeTemplate: "api/{area}/{controller}/{action}/{id}",
|
||||
routeTemplate: "api/{controller}/{action}/{id}",
|
||||
defaults: new { id = RouteParameter.Optional }
|
||||
);
|
||||
|
||||
#region 处理接口报错
|
||||
// 以下404处理不包括地址段错误的情况,如直接访问/aaa或/api/controller/action/id/aaa都会返回404错误页
|
||||
// 404
|
||||
config.Services.Replace(typeof(IHttpControllerSelector), new HttpNotFoundDefaultHttpControllerSelector(config));
|
||||
// 404/405
|
||||
config.Services.Replace(typeof(IHttpActionSelector), new HttpNotFoundControllerActionSelector(config));
|
||||
// 500
|
||||
config.Services.Replace(typeof(IHttpActionInvoker), new HttpWebApiControllerActionInvoker(config));
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Ewide.Core.WebApi.Areas.Base
|
||||
{
|
||||
public class BaseAreaRegistration : AreaRegistration
|
||||
{
|
||||
public override string AreaName
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Base";
|
||||
}
|
||||
}
|
||||
|
||||
public override void RegisterArea(AreaRegistrationContext context)
|
||||
{
|
||||
context.MapRoute(
|
||||
"Base_default",
|
||||
"Base/{controller}/{action}/{id}",
|
||||
new { action = "Index", id = UrlParameter.Optional }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace Ewide.Core.WebApi.Areas.Base.Controllers
|
||||
{
|
||||
[ValidateArgumentsFilter]
|
||||
public class BetestController : BaseController
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取所有接口及其注释
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Route("test22")]
|
||||
[HttpPost]
|
||||
public IHttpActionResult Test1()
|
||||
{
|
||||
var apis = Configuration.Services.GetApiExplorer().ApiDescriptions;
|
||||
|
||||
var result = apis
|
||||
.Select(p => new
|
||||
{
|
||||
p.RelativePath,
|
||||
p.Documentation
|
||||
});
|
||||
return DisplayJSON(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IHttpActionResult Test2()
|
||||
{
|
||||
return DisplayJSON("");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Ewide.Core.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace Ewide.Core.WebApi.Areas.Base.Controllers
|
||||
{
|
||||
[ValidateArgumentsFilter]
|
||||
public class GateController : BaseController
|
||||
{
|
||||
/// <summary>
|
||||
/// 登录
|
||||
/// </summary>
|
||||
/// <param name="dto"></param>
|
||||
/// <returns></returns>
|
||||
[ValidateArgumentsFilter(AllowNull = true)]
|
||||
public IHttpActionResult Login(LoginDTO dto)
|
||||
{
|
||||
return DisplayJSON(dto);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Views/web.config
Normal file
36
Api/Ewide.Core/Ewide.Core.WebApi/Areas/Base/Views/web.config
Normal file
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<configuration>
|
||||
<configSections>
|
||||
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
|
||||
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
|
||||
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.2.4.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="Ewide.Core.WebApi" />
|
||||
|
||||
</namespaces>
|
||||
</pages>
|
||||
</system.web.webPages.razor>
|
||||
|
||||
<appSettings>
|
||||
<add key="webpages:Enabled" value="false" />
|
||||
</appSettings>
|
||||
|
||||
<system.webServer>
|
||||
<handlers>
|
||||
<remove name="BlockViewHandler"/>
|
||||
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
|
||||
</handlers>
|
||||
</system.webServer>
|
||||
</configuration>
|
||||
@@ -34,7 +34,7 @@ namespace Ewide.Core.WebApi.Areas.HelpPage
|
||||
public static void Register(HttpConfiguration config)
|
||||
{
|
||||
//// Uncomment the following to use the documentation from XML documentation file.
|
||||
//config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));
|
||||
config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/bin/Ewide.Core.WebApi.xml")));
|
||||
|
||||
//// Uncomment the following to use "sample string" as the sample for all actions that have string as the body parameter or return type.
|
||||
//// Also, the string arrays will be used for IEnumerable<string>. The sample objects will be serialized into different media type
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace Ewide.Core.WebApi.Areas.Manage.Controllers
|
||||
{
|
||||
public class AreaTestController : BaseController
|
||||
{
|
||||
/// <summary>
|
||||
/// 区域测试
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IHttpActionResult Test()
|
||||
{
|
||||
return DisplayJSON("1");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.Results;
|
||||
@@ -15,7 +16,7 @@ namespace Ewide.Core.WebApi
|
||||
{
|
||||
protected override void Initialize(HttpControllerContext controllerContext)
|
||||
{
|
||||
//base.Initialize(controllerContext);
|
||||
base.Initialize(controllerContext);
|
||||
}
|
||||
|
||||
protected override ExceptionResult InternalServerError(Exception exception)
|
||||
|
||||
@@ -13,9 +13,25 @@ namespace Ewide.Core.WebApi
|
||||
{
|
||||
public class ValidateArgumentsFilter : ActionFilterAttribute
|
||||
{
|
||||
public bool AllowNull { get; set; } = false;
|
||||
|
||||
public override void OnActionExecuting(HttpActionContext actionContext)
|
||||
{
|
||||
if (!AllowNull)
|
||||
{
|
||||
foreach (var arg in actionContext.ActionArguments)
|
||||
{
|
||||
if (arg.Value == null)
|
||||
{
|
||||
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK,
|
||||
BaseDisplayJSON.DisplayJSON(BaseDisplayJSON.Display(HttpStatusCode.BadRequest, "参数不可为空"))
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!actionContext.ModelState.IsValid)
|
||||
{
|
||||
var message = new List<Object>();
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
using Ewide.Core.DTO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace Ewide.Core.WebApi.Controllers
|
||||
{
|
||||
[ValidateArgumentsFilter]
|
||||
public class GateController : BaseController
|
||||
{
|
||||
public IHttpActionResult Login(TestArgs A)
|
||||
{
|
||||
return DisplayJSON("");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>bin\Ewide.Core.WebApi.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -132,6 +133,9 @@
|
||||
<Compile Include="App_Start\Filters\HttpWebApiControllerActionInvoker.cs" />
|
||||
<Compile Include="App_Start\RouteConfig.cs" />
|
||||
<Compile Include="App_Start\WebApiConfig.cs" />
|
||||
<Compile Include="Areas\Base\BaseAreaRegistration.cs" />
|
||||
<Compile Include="Areas\Base\Controllers\BetestController.cs" />
|
||||
<Compile Include="Areas\Base\Controllers\GateController.cs" />
|
||||
<Compile Include="Areas\HelpPage\ApiDescriptionExtensions.cs" />
|
||||
<Compile Include="Areas\HelpPage\App_Start\HelpPageConfig.cs" />
|
||||
<Compile Include="Areas\HelpPage\Controllers\HelpController.cs" />
|
||||
@@ -160,11 +164,11 @@
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\SampleDirection.cs" />
|
||||
<Compile Include="Areas\HelpPage\SampleGeneration\TextSample.cs" />
|
||||
<Compile Include="Areas\HelpPage\XmlDocumentationProvider.cs" />
|
||||
<Compile Include="Areas\Manage\Controllers\AreaTestController.cs" />
|
||||
<Compile Include="Areas\Manage\ManageAreaRegistration.cs" />
|
||||
<Compile Include="Controllers\Code\ApiAuthorizeAttribute.cs" />
|
||||
<Compile Include="Controllers\Code\BaseController.cs" />
|
||||
<Compile Include="Controllers\Code\ValidateArgumentsFilter.cs" />
|
||||
<Compile Include="Controllers\GateController.cs" />
|
||||
<Compile Include="Controllers\HomeController.cs" />
|
||||
<Compile Include="Global.asax.cs">
|
||||
<DependentUpon>Global.asax</DependentUpon>
|
||||
@@ -219,7 +223,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="App_Data\" />
|
||||
<Folder Include="Areas\Manage\Controllers\" />
|
||||
<Folder Include="Areas\Base\Models\" />
|
||||
<Folder Include="Areas\Base\Views\Shared\" />
|
||||
<Folder Include="Areas\Manage\Models\" />
|
||||
<Folder Include="Areas\Manage\Views\Shared\" />
|
||||
<Folder Include="Models\" />
|
||||
@@ -250,17 +255,18 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Areas\Manage\Views\web.config" />
|
||||
<Content Include="Areas\Base\Views\web.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ewide.Core.Arguments\Ewide.Core.DTO.csproj">
|
||||
<Project>{9003B29C-AC1D-444E-8FE2-201F76D848A5}</Project>
|
||||
<Name>Ewide.Core.DTO</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Ewide.Core.Common\Ewide.Core.Common.csproj">
|
||||
<Project>{c7e2ac14-ac20-4552-a5b8-08b650ac8416}</Project>
|
||||
<Name>Ewide.Core.Common</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Ewide.Core.DTO\Ewide.Core.DTO.csproj">
|
||||
<Project>{590704FF-28C5-4536-B587-AC213858CC42}</Project>
|
||||
<Name>Ewide.Core.DTO</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Ewide.Core.Service\Ewide.Core.Service.csproj">
|
||||
<Project>{34ae80c2-5c37-4b6c-aac3-f52c06928721}</Project>
|
||||
<Name>Ewide.Core.Service</Name>
|
||||
|
||||
Reference in New Issue
Block a user