简体   繁体   English

Web api属性标签如何工作

[英]how does web api attribute tag work

new to c sharp, visual studio and web api. Sharp,Visual Studio和Web API的新功能。 (come from java). (来自Java)。

Anyways I'm playing around with web api from visual studio. 无论如何,我正在使用Visual Studio中的Web API。 In the ValuesControler class I notice it set something call a attribute on top of the class, so whenever a browser make a request to api/values it will need to be authorized first. ValuesControler类中,我注意到它在类的顶部设置了一些称为属性的属性,因此,每当浏览器向api/values请求时,都需要首先对其进行授权。

But what exactly is an attribute? 但是属性到底是什么?

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

I also found the routing attributes, but I cant find any info on what exactly is attributes and how is it getting read or understood by the program. 我也找到了路由属性,但是找不到关于什么是属性以及程序如何读取或理解属性的任何信息。

In c# Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). 在c#中, 属性提供了一种将声明性信息与C#代码(类型,方法,属性等)相关联的强大方法。 Once associated with a program entity, the attribute can be queried at run time and used in any number of ways. 一旦与程序实体相关联,就可以在运行时查询该属性,并以多种方式使用它。

For authorize attribute check out 对于授权属性签出

Authentication and Authorization in ASP.NET Web API ASP.NET Web API中的身份验证和授权

Using the [Authorize] Attribute 使用[授权]属性

Web API provides a built-in authorization filter, AuthorizeAttribute. Web API提供了内置的授权过滤器AuthorizeAttribute。 This filter checks whether the user is authenticated. 该过滤器检查用户是否已通过身份验证。 If not, it returns HTTP status code 401 (Unauthorized), without invoking the action. 如果不是,它将返回HTTP状态代码401(未授权),而不调用操作。

For attribute routing check out 对于属性路由,请检出

Attribute Routing in ASP.NET Web API 2 ASP.NET Web API 2中的属性路由

Routing is how Web API matches a URI to an action. 路由是Web API如何将URI与操作匹配的方式。 Web API 2 supports a new type of routing, called attribute routing. Web API 2支持一种新型的路由,称为属性路由。 As the name implies, attribute routing uses attributes to define routes. 顾名思义,属性路由使用属性来定义路由。 Attribute routing gives you more control over the URIs in your web API. 通过属性路由,您可以更好地控制Web API中的URI。 For example, you can easily create URIs that describe hierarchies of resources. 例如,您可以轻松创建描述资源层次结构的URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM