【问题标题】:how does web api attribute tag workweb api 属性标签是如何工作的
【发布时间】:2016-09-25 22:20:49
【问题描述】:

c sharp、visual studio 和 web api 的新手。 (来自java)。

无论如何,我都在使用 Visual Studio 的 web api。在ValuesControler 类中,我注意到它在类的顶部设置了一些调用属性,因此每当浏览器向api/values 发出请求时,都需要先获得授权。

但究竟什么是属性?

[Authorize]
public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

我还找到了路由属性,但我找不到任何关于属性到底是什么以及程序如何读取或理解它的信息。

【问题讨论】:

    标签: c# .net visual-studio asp.net-web-api attributes


    【解决方案1】:

    在c#中Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.

    对于授权属性检查

    Authentication and Authorization in ASP.NET Web API

    使用 [Authorize] 属性

    Web API 提供了一个内置的授权过滤器,AuthorizeAttribute。 此过滤器检查用户是否经过身份验证。如果没有,它 返回 HTTP 状态码 401(未授权),而不调用 行动。

    属性路由签出

    Attribute Routing in ASP.NET Web API 2

    路由是 Web API 将 URI 与操作匹配的方式。 Web API 2 支持 一种新的路由类型,称为属性路由。顾名思义, 属性路由使用属性来定义路由。属性路由 让您可以更好地控制 Web API 中的 URI。例如,你 可以轻松创建描述资源层次结构的 URI。

    【讨论】:

    • 是的,我之前已经阅读过该页面,我知道如何使用它。我要问的是“我找不到任何关于属性标签到底是什么以及程序如何读取或理解它的信息”。例如:它是一个对象,它是一个变量,它是什么?
    • OP 也可能会发现此链接很有用:stackoverflow.com/questions/21004932/…
    • 属性基本上是类的元数据。框架将通过反射在其功能的执行中使用这些信息。
    • 谢谢两位,这就是我要找的东西
    • 当我试图理解这个主题时,我发现 Andras Nemes 关于 WebApi 的文章非常有用:dotnetcodr.com/mvc4-various
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多