Files
laatzen/LaaProductionWeb/LaaProduction.Web/App_Infrastructure/RouteParameterAttribute.cs
T

41 lines
1.2 KiB
C#

namespace LaaProduction.Web.App_Infrastructure
{
using Newtonsoft.Json;
using System.Linq;
using System.Web.Mvc;
public class RouteParameterAttribute : ActionFilterAttribute
{
private readonly string parameterName;
public RouteParameterAttribute(string parameterName)
=> this.parameterName = parameterName;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (filterContext.ActionParameters.ContainsKey(this.parameterName))
{
var parameter = filterContext
.ActionDescriptor
.GetParameters()
.FirstOrDefault();
var parameterValue = parameter.DefaultValue;
try
{
var routeValue = filterContext
.RouteData
.GetRequiredString("id");
parameterValue = JsonConvert.DeserializeObject(routeValue, parameter.ParameterType);
}
catch { }
filterContext.ActionParameters[this.parameterName] = parameterValue;
}
}
}
}