如何获取默认网址“ http://localhost:53037/api/about”而不是“ http://localhost:53037/about?
WebAPIConfig.cs:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
HomeController.cs:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
}
答案 0 :(得分:0)
在不带api文本的默认值下方添加一个MapHttpRoute。
config.Routes.MapHttpRoute(
name: "CustomApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
现在您可以在不使用api文本的情况下调用API。