kma*_*man 5 asp.net webforms asp.net-web-api
正如标题所述,我想知道您是否可以使用基于属性的WebAPI 2与WebForms的路由.我觉得这显然可以完成,因为你可以在WebForms应用程序中使用WebAPI2 ......我只是无法弄清楚如何启用基于属性的路由.
基于这篇文章,我了解到你通常在设置基于约定的路由之前通过调用MapHttpAttributeRoutes()来启用它.但我猜这是MVC的方式 - 我需要知道WebForms的等价物.
我目前使用MapHttpRoute()来设置基于约定的路由,我想在WebAPI2中尝试基于属性的路由.我用WebAPI2更新了我的项目 - 我只需要知道如何启用基于属性的路由功能.
任何信息,将不胜感激.
在WebForms的情况下,您不需要做任何特殊的事情.Web API属性路由应该像在MVC中一样工作.
如果您使用的是VS 2013,则可以使用"Web窗体"模板创建项目,然后选择"Web API"复选框,轻松测试,您应该看到由此生成的以下所有代码.
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
Global.asax中
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Run Code Online (Sandbox Code Playgroud)
WebForm的RouteConfig
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1766 次 |
| 最近记录: |