You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.4 KiB
40 lines
1.4 KiB
using System;
|
|
using Microsoft.AspNetCore.Mvc.Abstractions;
|
|
using Microsoft.AspNetCore.Mvc.ActionConstraints;
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
namespace Mvc.Server.Helpers
|
|
{
|
|
public sealed class FormValueRequiredAttribute : ActionMethodSelectorAttribute
|
|
{
|
|
private readonly string _name;
|
|
|
|
public FormValueRequiredAttribute(string name)
|
|
{
|
|
_name = name;
|
|
}
|
|
|
|
public override bool IsValidForRequest(RouteContext context, ActionDescriptor action)
|
|
{
|
|
if (string.Equals(context.HttpContext.Request.Method, "GET", StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(context.HttpContext.Request.Method, "HEAD", StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(context.HttpContext.Request.Method, "DELETE", StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(context.HttpContext.Request.Method, "TRACE", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(context.HttpContext.Request.ContentType))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!context.HttpContext.Request.ContentType.StartsWith("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return !string.IsNullOrEmpty(context.HttpContext.Request.Form[_name]);
|
|
}
|
|
}
|
|
}
|
|
|