[ASP.NET] Web Service (.asmx) 遇到404問題
昨天在傳統Web Service (.asmx)上遇到直接存取.asmx頁面正常,可秀出Documentation說明(如果Web.Config沒有將Documentation remove掉的話),但POST/GET過去IIS會回應404 Not Found的問題。
查了老半天,才發現是專案類型為ASP.NET MVC專案導致的,會將:
http://{site}/Foo.asmx/Bar
這類的URL,以MVC的路由來處理,因為找不到對應的Controller,自然就得到404。
解決方法是在 RouteConfig.cs 新增 .asmx 檔的處理規則:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}