问题折这样发生的,我们公司有一堆的企业微信应用,
每一个应用对应一个控制器,接入嘛,当然使用一个抽象的基类: 里面定义一个接入 和 接收消息 2个接口就可以了。。
///企业微信接入
[HttpGet]
[ActionName("Index")]
public ActionResult Get(
string msg_signature = "",
string timestamp = "",
string nonce = "",
string echostr = "") ....
///接收微信的通知消息
[HttpPost]
[ActionName("Index")]
public ActionResult Post(PostModel postModel)
噗通的是,现在我们需要接入外部公司的企业微信了,接入公司数量不定;
这个貌似也简单,
///企业微信接入
[HttpGet]
// [ActionName("Index")]
[Route("Index/{state=?}")]
public ActionResult Get(
string state,
string msg_signature = "",
string timestamp = "",
string nonce = "",
string echostr = "") ....
///接收微信的通知消息
[HttpPost]
// [ActionName("Index")]
[Route("Index/{state=?}")]
public ActionResult Post(string state, PostModel postModel)
但是,为毛 404 出来了??呃,
貌似 Route 这个东东会遇到少少的麻烦, 这样的写法,貌似只能通过 http://xxx/Index/abcd 的形式访问。。
改成 [Route("{controller}/Index/{state=?}")]
照旧。。404.。
在每一个 控制器下增加
[RoutePrefix("TestWechatWork")]
public class TestWechatWorkController : AbstractWorkMessageController<TestWechatWorkMessageHandler>
把上面变回为 [Route("Index/{state=?}")]
还是 404;
MVC这个 Route 好奇怪,
一个 Action, 如果定义在父类中,则子类如果不重载这个方法,这个 Action 将:
a. 父类的方法中定义了 [NonAction], 子类中照样会暴露这个方法
b. 父类方法中定义了 [Route] , 子类定义了 [RoutePrefix] 标记;时,此[Prefix] 对父类中的 [Route] 不起作用;
一个控制器中定义了一个泛型方法,然后使用 [NonAction] 进行标注,则:
a. 告诉你不能输出泛型方法,毛啊,我不要输出啊,我明明定义了 [NonAction] 标记吖。。
扫了一下 RouteCollection 中的方法,发现一个方法:
对命名空间添加路由映射,
// 对于命名空间 EhayWebApi.WechatWork 下的类,增加映射 state 的路由映射
// 没有此参数,则 http://localhost/ne/DeviceDevelopMessage/index/水机推广 这个url中 水机推广无法映射到 state 参数中;
routes.MapRoute(
name: "WechatWork",
url: "{controller}/{action}/{state}",
defaults: new
{
action = "Index",
state = UrlParameter.Optional
},
namespaces: new string[]{
"EhayWebApi.WechatWork"
}
);