This commit is contained in:
ky_sunl
2021-03-18 10:07:33 +00:00
parent 299d03bdb0
commit 05b6aa1674
19 changed files with 323 additions and 56 deletions

View File

@@ -43,6 +43,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="LoginDTO.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestArgs.cs" />
</ItemGroup>

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ewide.Core.DTO
{
public class LoginDTO
{
[DisplayName("登录帐号")]
[Required(AllowEmptyStrings = false, ErrorMessage = "登录帐号不可为空")]
public string Account { get; set; }
[DisplayName("登录密码")]
[Required(AllowEmptyStrings = false, ErrorMessage = "登录密码不可为空")]
public string Password { get; set; }
}
}

View File

@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{590704FF-28C5-4536-B587-AC213858CC42}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Ewide.Core.DTO</RootNamespace>
<AssemblyName>Ewide.Core.DTO</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="LoginDTO.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ewide.Core.DTO
{
public class LoginDTO
{
[DisplayName("登录帐号")]
[Phone(ErrorMessage = "a a")]
[Required(AllowEmptyStrings = false, ErrorMessage = "登录帐号不可为空")]
public string Account { get; set; }
[DisplayName("登录密码")]
[Required(AllowEmptyStrings = false, ErrorMessage = "登录密码不可为空")]
public string Password { get; set; }
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Ewide.Core.DTO")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Ewide.Core.DTO")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("590704ff-28c5-4536-b587-ac213858cc42")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -45,10 +45,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</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>

View File

@@ -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);

View File

@@ -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
}
}
}

View File

@@ -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 }
);
}
}
}

View File

@@ -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("");
}
}
}

View File

@@ -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);
}
}
}

View 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>

View File

@@ -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

View File

@@ -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");
}
}
}

View File

@@ -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)

View File

@@ -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>();

View File

@@ -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("");
}
}
}

View File

@@ -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>

View File

@@ -17,12 +17,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ewide.Core.Utility", "Ewide
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ewide.Core.Service", "Ewide.Core.Service\Ewide.Core.Service.csproj", "{34AE80C2-5C37-4B6C-AAC3-F52C06928721}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ewide.Core.DTO", "Ewide.Core.Arguments\Ewide.Core.DTO.csproj", "{9003B29C-AC1D-444E-8FE2-201F76D848A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ewide.Core.Common", "Ewide.Core.Common\Ewide.Core.Common.csproj", "{C7E2AC14-AC20-4552-A5B8-08B650AC8416}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ewide.Core.Data", "Ewide.Core.Data\Ewide.Core.Data.csproj", "{B5B46BAD-81E3-4DF0-83EF-75148236F7CE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ewide.Core.DTO", "Ewide.Core.DTO\Ewide.Core.DTO.csproj", "{590704FF-28C5-4536-B587-AC213858CC42}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -45,10 +45,6 @@ Global
{34AE80C2-5C37-4B6C-AAC3-F52C06928721}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34AE80C2-5C37-4B6C-AAC3-F52C06928721}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34AE80C2-5C37-4B6C-AAC3-F52C06928721}.Release|Any CPU.Build.0 = Release|Any CPU
{9003B29C-AC1D-444E-8FE2-201F76D848A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9003B29C-AC1D-444E-8FE2-201F76D848A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9003B29C-AC1D-444E-8FE2-201F76D848A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9003B29C-AC1D-444E-8FE2-201F76D848A5}.Release|Any CPU.Build.0 = Release|Any CPU
{C7E2AC14-AC20-4552-A5B8-08B650AC8416}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7E2AC14-AC20-4552-A5B8-08B650AC8416}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7E2AC14-AC20-4552-A5B8-08B650AC8416}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -57,6 +53,10 @@ Global
{B5B46BAD-81E3-4DF0-83EF-75148236F7CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5B46BAD-81E3-4DF0-83EF-75148236F7CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5B46BAD-81E3-4DF0-83EF-75148236F7CE}.Release|Any CPU.Build.0 = Release|Any CPU
{590704FF-28C5-4536-B587-AC213858CC42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{590704FF-28C5-4536-B587-AC213858CC42}.Debug|Any CPU.Build.0 = Debug|Any CPU
{590704FF-28C5-4536-B587-AC213858CC42}.Release|Any CPU.ActiveCfg = Release|Any CPU
{590704FF-28C5-4536-B587-AC213858CC42}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -65,9 +65,9 @@ Global
{31C3CA3D-14A1-453A-866D-76D4C74A9BDC} = {30FD9EAF-7D70-4244-83AD-EB702BDC95ED}
{D5C48D01-5AB1-44C9-8709-D4A336D19E9F} = {2D369B73-E5F6-412B-AD86-C5D1384D815B}
{34AE80C2-5C37-4B6C-AAC3-F52C06928721} = {62222202-F9F0-4E0A-9C7F-CB69CBE60611}
{9003B29C-AC1D-444E-8FE2-201F76D848A5} = {30FD9EAF-7D70-4244-83AD-EB702BDC95ED}
{C7E2AC14-AC20-4552-A5B8-08B650AC8416} = {2D369B73-E5F6-412B-AD86-C5D1384D815B}
{B5B46BAD-81E3-4DF0-83EF-75148236F7CE} = {30FD9EAF-7D70-4244-83AD-EB702BDC95ED}
{590704FF-28C5-4536-B587-AC213858CC42} = {30FD9EAF-7D70-4244-83AD-EB702BDC95ED}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {435CD121-36FA-41DC-95FE-0B93517A1190}