c#-ASP.NET Web API:如果从资源中设置了错误消息,则模型有效
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-ASP.NET Web API:如果从资源中设置了错误消息,则模型有效,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4184字,纯文字阅读大概需要6分钟。
内容图文
![c#-ASP.NET Web API:如果从资源中设置了错误消息,则模型有效](/upload/InfoBanner/zyjiaocheng/658/3ceb25577b9348d4bbd0347b823a8890.jpg)
问题是,如果我使用.rsx文件(资源)来提供自定义错误消息,则在ApiController中,ModelState.IsValid始终为true.
这是我的模型:
public class LoginModel
{
public string Email { get; set; }
[Required]
[MinLength(5)]
public string Password { get; set; }
}
ApiController中的方法:
[HttpPost]
[ModelValidationFilter]
public void Post(LoginModel model)
{
var a = ModelState.IsValid;
}
和过滤器:
public class ModelValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
我在POST请求中发送此消息:
{ Email: "asd@asd.com", Password: "a" }
ModelState.IsValid为false,并且响应符合预期:
{
"Message": "The request is invalid.",
"ModelState":
{
"model.Password":
[
"The field Password must be a string or array type with a minimum length of '5'."
]
}
}
但是,如果我在验证属性中使用资源(配置为“公共和嵌入式资源”构建操作):
public class LoginModel
{
public string Email { get; set; }
[Required]
[MinLength(5, ErrorMessageResourceName = "Test", ErrorMessageResourceType = typeof(Resources.Localization))]
public string Password { get; set; }
}
(“测试”键仅保留“测试”字符串值)
ModelState.IsValid为true.
资源类是可见的,并且重新共享程序会正确验证ErrorMessageResourceName中提供的字符串.
解决方法:
我尝试了您的解决方案,但我不理解类Resources.Localization.它从哪里来的?
我的解决方案如下所述,并且使用了良好的资源.
模型:
using TestApp.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace TestApp.Models
{
public class LoginModel
{
public string Email { get; set; }
[Required]
[MinLength(5, ErrorMessageResourceName="Test", ErrorMessageResourceType=typeof(Resources))]
public string Password { get; set; }
}
}
ModelValidationFilterAttribute:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Net.Http;
namespace TestApp.Controllers
{
public class ModelValidationFilterAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
}
两个资源文件,一个是常规的Resources.resx,其中包含字符串键/值Test / someth;另一个是Resources.de-DE.resx,其中包含字符串键/值Test / something_DE.
使用提琴手我发送了这个:
Header:
User-Agent: Fiddler
Host: localhost:63315
Content-Length: 37
Content-Type: application/json
身体:
{Email:"text@test.com", Password:"a"}
响应为字符串:
HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcV29ya1xDU1xGYkJpcnRoZGF5QXBwXEZiQmRheUFwcDJcYXBpXGxvZ2lu?=
X-Powered-By: ASP.NET
Date: Fri, 28 Jun 2013 14:56:54 GMT
Content-Length: 83
{"Message":"The request is invalid.","ModelState":{"model.Password":["something"]}}
对于de-DE,请求标头为:
User-Agent: Fiddler
Host: localhost:63315
Content-Length: 37
Accept-Language: de-DE
Content-Type: application/json
回应是:
HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcV29ya1xDU1xGYkJpcnRoZGF5QXBwXEZiQmRheUFwcDJcYXBpXGxvZ2lu?=
X-Powered-By: ASP.NET
Date: Fri, 28 Jun 2013 14:57:39 GMT
Content-Length: 86
{"Message":"The request is invalid.","ModelState":{"model.Password":["something_DE"]}}
如您所见,消息是从本地化资源文件中读取的,带有“ _DE”.
这是您想要实现的目标吗?
亲切的问候.
编辑:
路由配置原为
config.Routes.MapHttpRoute(
name: "LoginRoute",
routeTemplate: "api/login",
defaults: new { controller = "Login", action = "PostLogin" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
);
Web.config添加了下一部分:
<system.web>
...
<globalization culture="auto" uiCulture="auto" />
...
</system.web>
内容总结
以上是互联网集市为您收集整理的c#-ASP.NET Web API:如果从资源中设置了错误消息,则模型有效全部内容,希望文章能够帮你解决c#-ASP.NET Web API:如果从资源中设置了错误消息,则模型有效所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。