BN089-BS开发框架MVC4版/source20131023/Zephyr.Web/App_Start/webstack/StartWithConstraint.cs
(完整代码) 2KBusing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Routing;
namespace Zephyr.Web
{
/// <summary>
/// 如果请求url如: api/area/controller/x x有可能是actioin或id
/// 在url中的x位置出现的是以 get put delete post开头的字符串,则当作action,否则就当作id
/// 如果action为空,则把请求方法赋给action
/// </summary>
public class StartWithConstraint : IHttpRouteConstraint
{
public string[] array { get; set; }
public bool match { get; set; }
private string _id = "id";
public StartWithConstraint(string[] startwithArray = null)
{
if (startwithArray == null)
startwithArray = new string[] { "GET", "PUT", "DELETE", "POST", "EDIT", "UPDATE", "AUDIT", "DOWNLOAD" };
this.array = startwithArray;
}
public bool Match(System.Net.Http.HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
if (values == null) // shouldn't ever hit this.
return true;
if (!values.ContainsKey(parameterName) || !values.ContainsKey(_id)) // make sure the parameter is there.
return true;
var action = values[parameterName].ToString().ToLower();
if (string.IsNullOrEmpty(action)) // if the param key is empty in this case "action" add the method so it doesn't hit other methods like "GetStatus"
{
values[parameterName] = request.Method.ToString();
}
else if (string.IsNullOrEmpty(values[_id].ToString()))
{
var isidstr = true;
array.ToList().ForEach(x =>
{
if (action.StartsWith(x.ToLower()))
isidstr = false;
});
if (isidstr)
{
values[_id] = values[parameterName];
values[parameterName] = request.Method.ToString();
}
}
return true;
}
}
}
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。
