16
16
using Microsoft . AspNetCore . Mvc . Routing ;
17
17
using Microsoft . AspNetCore . Routing ;
18
18
using Microsoft . Extensions . DependencyInjection ;
19
+ using Microsoft . Extensions . Options ;
19
20
using Microsoft . Net . Http . Headers ;
20
21
21
22
namespace Microsoft . AspNetCore . Mvc
@@ -1821,6 +1822,52 @@ public virtual ConflictObjectResult Conflict([ActionResultObjectValue] object er
1821
1822
public virtual ConflictObjectResult Conflict ( [ ActionResultObjectValue ] ModelStateDictionary modelState )
1822
1823
=> new ConflictObjectResult ( modelState ) ;
1823
1824
1825
+ /// <summary>
1826
+ /// Creates an <see cref="ObjectResult"/> that produces a <see cref="ProblemDetails"/> response with a <c>500</c>
1827
+ /// error status with a <see cref="ProblemDetails" /> value.
1828
+ /// </summary>
1829
+ /// <param name="title">The value for <see cref="ProblemDetails.Title" />.</param>
1830
+ /// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
1831
+ /// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
1832
+ /// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
1833
+ /// <returns>The created <see cref="ObjectResult"/> for the response.</returns>
1834
+ [ NonAction ]
1835
+ public virtual ObjectResult Problem (
1836
+ string detail = null ,
1837
+ string instance = null ,
1838
+ string title = null ,
1839
+ string type = null )
1840
+ {
1841
+ var problemDetails = new ProblemDetails
1842
+ {
1843
+ Title = title ,
1844
+ Type = type ,
1845
+ Detail = detail ,
1846
+ Instance = instance ,
1847
+ } ;
1848
+
1849
+ ApplyProblemDetailsDefaults ( problemDetails , statusCode : 500 ) ;
1850
+
1851
+ return new ObjectResult ( problemDetails ) ;
1852
+ }
1853
+
1854
+ private void ApplyProblemDetailsDefaults ( ProblemDetails problemDetails , int statusCode )
1855
+ {
1856
+ problemDetails . Status = statusCode ;
1857
+
1858
+ if ( problemDetails . Title is null || problemDetails . Type is null )
1859
+ {
1860
+ var options = HttpContext . RequestServices . GetRequiredService < IOptions < ApiBehaviorOptions > > ( ) . Value ;
1861
+ if ( options . ClientErrorMapping . TryGetValue ( statusCode , out var clientErrorData ) )
1862
+ {
1863
+ problemDetails . Title ??= clientErrorData . Title ;
1864
+ problemDetails . Type ??= clientErrorData . Link ;
1865
+ }
1866
+ }
1867
+
1868
+ ProblemDetailsClientErrorFactory . SetTraceId ( ControllerContext , problemDetails ) ;
1869
+ }
1870
+
1824
1871
/// <summary>
1825
1872
/// Creates an <see cref="BadRequestObjectResult"/> that produces a <see cref="StatusCodes.Status400BadRequest"/> response.
1826
1873
/// </summary>
@@ -1837,8 +1884,10 @@ public virtual ActionResult ValidationProblem([ActionResultObjectValue] Validati
1837
1884
}
1838
1885
1839
1886
/// <summary>
1840
- /// Creates an <see cref="BadRequestObjectResult"/> that produces a <see cref="StatusCodes.Status400BadRequest"/> response.
1887
+ /// Creates an <see cref="BadRequestObjectResult"/> that produces a <see cref="StatusCodes.Status400BadRequest"/> response
1888
+ /// with validation errors from <paramref name="modelStateDictionary"/>.
1841
1889
/// </summary>
1890
+ /// <param name="modelStateDictionary">The <see cref="ModelStateDictionary"/>.</param>
1842
1891
/// <returns>The created <see cref="BadRequestObjectResult"/> for the response.</returns>
1843
1892
[ NonAction ]
1844
1893
public virtual ActionResult ValidationProblem ( [ ActionResultObjectValue ] ModelStateDictionary modelStateDictionary )
@@ -1849,6 +1898,8 @@ public virtual ActionResult ValidationProblem([ActionResultObjectValue] ModelSta
1849
1898
}
1850
1899
1851
1900
var validationProblem = new ValidationProblemDetails ( modelStateDictionary ) ;
1901
+ ApplyProblemDetailsDefaults ( validationProblem , statusCode : 400 ) ;
1902
+
1852
1903
return new BadRequestObjectResult ( validationProblem ) ;
1853
1904
}
1854
1905
@@ -1858,9 +1909,44 @@ public virtual ActionResult ValidationProblem([ActionResultObjectValue] ModelSta
1858
1909
/// </summary>
1859
1910
/// <returns>The created <see cref="BadRequestObjectResult"/> for the response.</returns>
1860
1911
[ NonAction ]
1861
- public virtual ActionResult ValidationProblem ( )
1912
+ public virtual ActionResult ValidationProblem ( ) => ValidationProblem ( ModelState ) ;
1913
+
1914
+ /// <summary>
1915
+ /// Creates an <see cref="BadRequestObjectResult"/> that produces a <see cref="StatusCodes.Status400BadRequest"/> response
1916
+ /// with a <see cref="ValidationProblemDetails"/> value.
1917
+ /// </summary>
1918
+ /// <param name="title">The value for <see cref="ProblemDetails.Title" />.</param>
1919
+ /// <param name="type">The value for <see cref="ProblemDetails.Type" />.</param>
1920
+ /// <param name="detail">The value for <see cref="ProblemDetails.Detail" />.</param>
1921
+ /// <param name="instance">The value for <see cref="ProblemDetails.Instance" />.</param>
1922
+ /// <param name="modelStateDictionary">The <see cref="ModelStateDictionary"/>.
1923
+ /// When <see langword="null"/> uses <see cref="ModelState"/>.</param>
1924
+ /// <returns>The created <see cref="BadRequestObjectResult"/> for the response.</returns>
1925
+ [ NonAction ]
1926
+ public virtual ActionResult ValidationProblem (
1927
+ string detail ,
1928
+ string instance = null ,
1929
+ string title = null ,
1930
+ string type = null ,
1931
+ [ ActionResultObjectValue ] ModelStateDictionary modelStateDictionary = null )
1862
1932
{
1863
- var validationProblem = new ValidationProblemDetails ( ModelState ) ;
1933
+ modelStateDictionary ??= ModelState ;
1934
+
1935
+ var validationProblem = new ValidationProblemDetails ( modelStateDictionary )
1936
+ {
1937
+ Detail = detail ,
1938
+ Instance = instance ,
1939
+ Type = type ,
1940
+ } ;
1941
+
1942
+ if ( title != null )
1943
+ {
1944
+ // ValidationProblemDetails has a Title by default. Do not overwrite it with a null
1945
+ validationProblem . Title = title ;
1946
+ }
1947
+
1948
+ ApplyProblemDetailsDefaults ( validationProblem , statusCode : 400 ) ;
1949
+
1864
1950
return new BadRequestObjectResult ( validationProblem ) ;
1865
1951
}
1866
1952
0 commit comments