问题描述
正如标题状态,我想知道是否可以使用的WebAPI 2的基于属性的路由与的WebForms。我喜欢这种感觉显然可以做给你可以使用WebAPI2就好在WebForms的应用程序...我只是无法弄清楚如何启用基于属性的路由。
As the title states, I'm wondering if you can use the attribute-based routing of WebAPI 2 with WebForms. I feel like this can obviously be done given you can use WebAPI2 just fine in a WebForms application... I just can't figure out how to enable attribute-based routing.
在此基础上文章,我明白你通常通过到MapHttpAttributeRoutes()的调用之前设置您的基于约定的路由启用它。但我猜这是MVC的方式 - 我需要知道的WebForms相当于
Based on this article, I understand you normally enable it via a call to MapHttpAttributeRoutes() prior to setting up your convention-based routes. But I'm guessing this is the MVC way - I need to know the equivalent for WebForms.
我目前使用MapHttpRoute()来建立我以公约为基础的路线,我想尝试在WebAPI2基于属性的路由。我已经更新我的项目,WebAPI2 - 我只需要知道如何启用基于属性的路由功能
I currently use MapHttpRoute() to set up my convention-based routes, and I'd like to try out the attribute-based routing in WebAPI2. I have updated my project with WebAPI2 - I just need to know how to enable the attribute-based routing feature.
任何信息将是AP preciated。
Any info would be appreciated.
推荐答案
您不需要做什么特别的WebForms的情况。网页API属性路径应该工作就像在MVC。
You need not do anything special in case of WebForms. Web API attribute routing should work just as in MVC.
如果您使用的是VS 2013,你可以测试这一点很容易通过使用Web窗体创建项目模板,然后选择网络API复选框,然后你会看到这样产生以下所有code
If you are using VS 2013, you can test this easily by create a project using "Web Forms" template and then choose "Web API" check box and you should see all the following code generated by this.
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 }
);
}
}
的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);
}
}
的WebForm的RouteConfig
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
}
这篇关于你可以使用的WebAPI 2与WebForms的基于属性的路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!