在 ASP.NET 中使用 Web API 比使用 Web 方法有什么优势 [英] What is the advantage of using web API over web method in ASP.NET

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

问题描述

我熟悉网络方法.现在我得到了使用 Web API 而不是 Web 方法的建议.我已经完成了 ASP.NET Web API 的演示,它更接近 MVC 架构,正在使用经典的 ASP.NET Web 开发.我不喜欢将控制器(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 Web 服务(您称之为 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天全站免登陆