在ASP.NET中使用Web API而不是Web方法的优点是什么 [英] What is the advantage of using web API over web method in ASP.NET

查看:180
本文介绍了在ASP.NET中使用Web API而不是Web方法的优点是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉网络方法.现在,我得到了一个建议,使用Web API代替Web方法.我已经完成了一个ASP.NET Web API演示,它使用经典的asp.net Web开发更接近 MVC架构.我不喜欢将 controller (MVC概念)与经典开发混为一谈.

I am familiar with web method. Now I got a suggestion to use web API instead of web method. I had done a demo of ASP.NET web API it's more closer to a MVC architecture am using the classical asp.net web development. I don't like to mess up the controller (MVC concept) with classical development.

我的网络方法:

[WebMethod]
public static string GetName(int id)
{
    return "testName";
}

我的Web API控制器:

My Web API controller:

public class MyController : ApiController
{
[HttpGet]
public string GetName(int id)
{
    return "testName";
}
}

在这个问题上,我真的很困惑,任何人在同一问题上都有一个更好的主意.

am really confused on this issue any one have a better idea on the same.

您对此有何建议,哪个是更好的选择?

What is your suggestion on the same which is the better option?

如果两者都具有相同的代码段,我该如何比较?

How can i compare, if both having same piece of code?

推荐答案

经典的ASP.NET WebServices(您称为 WebMethod )是不推荐使用的技术.不再有任何积极的发展. ASP.NET Web API是对Microsoft Web堆栈的完全重写,在其中您可以更好地控制创建RESTful Web服务.这并不意味着您应该在一个或另一个之间进行选择.还有 ServiceStack .如果您要开始一个新项目,则应该远离经典的Web服务.如果它们仍然存在于.NET框架中,则是出于与旧版代码兼容的原因.

The classic ASP.NET WebServices (what you call WebMethod) are a deprecated technology. There is no longer any active development. The ASP.NET Web API is a complete rewrite of the web stack from Microsoft in which you have far greater control for creating RESTful web services. This doesn't mean that you should choose between one or the other. There's also ServiceStack. If you are starting a new project you should stay away from classic webservices. If they are still present in the .NET framework it is for compatibility reasons with legacy code.

这篇关于在ASP.NET中使用Web API而不是Web方法的优点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆