forked from aspnet/Mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ModelStateError if there is no input formatter selected.
- Loading branch information
1 parent
9faca78
commit 313a537
Showing
9 changed files
with
230 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
test/WebSites/FormatterWebSite/Controllers/InputFormatterController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNet.Mvc; | ||
|
||
namespace FormatterWebSite.Controllers | ||
{ | ||
public class InputFormatterController : Controller | ||
{ | ||
[HttpPost] | ||
public object ActionHandlesError([FromBody] DummyClass dummy) | ||
{ | ||
if (!ActionContext.ModelState.IsValid) | ||
{ | ||
var parameterBindingErrors = ActionContext.ModelState["dummy"].Errors; | ||
if (parameterBindingErrors.Count != 0) | ||
{ | ||
return new ErrorInfo | ||
{ | ||
ActionName = "ActionHandlesError", | ||
ParameterName = "dummy", | ||
Errors = parameterBindingErrors.Select(x => x.ErrorMessage).ToList(), | ||
Source = "action" | ||
}; | ||
} | ||
} | ||
|
||
return dummy; | ||
} | ||
|
||
[HttpPost] | ||
[ValidateBodyParameter] | ||
public object ActionFilterHandlesError([FromBody] DummyClass dummy) | ||
{ | ||
return dummy; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace FormatterWebSite | ||
{ | ||
public class ErrorInfo | ||
{ | ||
public string Source { get; set; } | ||
|
||
public string ActionName { get; set; } | ||
|
||
public string ParameterName { get; set; } | ||
|
||
public List<string> Errors { get; set; } | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
test/WebSites/FormatterWebSite/ValidateBodyParameterAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Linq; | ||
using Microsoft.AspNet.Mvc; | ||
|
||
namespace FormatterWebSite | ||
{ | ||
public class ValidateBodyParameterAttribute : ActionFilterAttribute | ||
{ | ||
public override void OnActionExecuting(ActionExecutingContext context) | ||
{ | ||
if (!context.ModelState.IsValid) | ||
{ | ||
var bodyParameter = context.ActionDescriptor | ||
.Parameters | ||
.FirstOrDefault(parameter => parameter.BodyParameterInfo != null); | ||
if (bodyParameter != null) | ||
{ | ||
var parameterBindingErrors = context.ModelState[bodyParameter.Name].Errors; | ||
if (parameterBindingErrors.Count != 0) | ||
{ | ||
var errorInfo = new ErrorInfo | ||
{ | ||
ActionName = context.ActionDescriptor.Name, | ||
ParameterName = bodyParameter.Name, | ||
Errors = parameterBindingErrors.Select(x => x.ErrorMessage).ToList(), | ||
Source = "filter" | ||
}; | ||
|
||
context.Result = new ObjectResult(errorInfo); | ||
} | ||
} | ||
} | ||
|
||
base.OnActionExecuting(context); | ||
} | ||
} | ||
} |