在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.