当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET Core中的Action的返回值类型

ASP.NET Core中的Action的返回值类型

2020年04月20日  | 移动技术网IT编程  | 我要评论

温柔前夫 别耍酷,www.ymg.cc,热腾

在asp.net core之前所有的action返回值都是actionresult,json(),file()等方法返回的都是actionresult的子类。并且core把mvc跟webapi合并之后action的返回值体系也有了很大的变化。

actionresult类

actionresult类是最常用的返回值类型。基本沿用了之前asp.net mvc的那套东西,使用它大部分情况都没问题。比如用它来返回视图,返回json,返回文件等等。如果是异步则使用task

    public class testcontroller : controller
    {
        public actionresult index()
        {
            return view();
        }

        public actionresult myfile()
        {
            return file(new byte[] { }, "image/jpg");
        }

        public actionresult myjson()
        {
            return json(new { name = "json" });
        }

        public actionresult ok()
        {
            return ok();
        }
    }

iactionresult接口

actionresult类实现了iactionresult接口所以能用actionresult的地方都可以使用iactionresult来替换。同样异步的话使用task包起来做为返回值。

   public class itestcontroller : controller
    {
        public iactionresult index()
        {
            return view();
        }

        public iactionresult myfile()
        {
            return file(new byte[] { }, "image/jpg");
        }

        public iactionresult myjson()
        {
            return json(new { name = "json" });
        }

        public iactionresult httpok()
        {
            return ok();
        }

        public async task<iactionresult> asynccall()
        {
            await task.delay(1000);

            return content("ok");
        }
    }

直接返回poco类

asp.net core的controller的action可以把poco类型(其实不一定是poco类,可以是任意类型,但是使用的时候一般都返回viwemodel等poco类)当做返回值,不一定非要是actionresult或者iactionresult。asp.net core框架会帮我们自动序列化返回给前端,默认使用json序列化。同样异步的话使用task包起来做为返回值。

   public class person
    {
        public string name { get; set; }

        public string sex { get; set; }
    }

    public class itestcontroller : controller
    {

          public person getperson()
        {
            return new person { name = "abc", sex = "f" };
        }

        public async task<list<person>> getpersons()
        {
            await task.delay(1000);

            return new list<person> {
            new person { name = "abc", sex = "f" },
            new person { name = "efg", sex = "m" }
            };
        }
    }

actionresult< t >泛型类

当我们设计restful webapi系统的时候习惯使用poco做为返回值。比如我们设计一个获取person的api。通过 /person/001 url获取001号person。

    [route("[controller]")]
    public class personcontroller : controller
    {
        ipersonrepository _repository;
        personcontroller(ipersonrepository repository) 
        {
            _repository = repository;
        }

        [httpget("{id}")]
       public person get(string id)
        {
            return _repository.get(id);
        }
    }

这个方法看起来好像没什么问题,但其实有个小问题。如果repository.get方法没有根据id查找到数据,那么将会返回null。如果null做为action的返回值,最后框架会转换为204的http status code。

204表示no content 。做为restful api,204的语义在这里会有问题,这里比较适合的status code是404 not found 。那么我们来改一下:

        [httpget("{id}")]
       public person get(string id)
        {
            var person = _repository.get(id);
            if (person == null)
            {
                response.statuscode = 404;
            }

            return person;
        }

现在如果查找不到person数据,则系统会返回404 not found 。

但是这看起来显然不够优雅,因为controllerbase内置了notfoundresult notfound() 方法。这使用这个方法代码看起来更加清晰明了。继续改:

        [httpget("{id}")]
       public person get(string id)
        {
            var person = _repository.get(id);
            if (person == null)
            {
                return notfound();
            }
            return person;
        }

很不幸,这段代码vs会提示错误。因为返回值类型不一致。方法签名的返回值是person,但是方法内部一会返回notfoundresult,一会返回person。

解决这个问题就该actionresult< t >出场了。我们继续改一下:

        [httpget("{id}")]
       public actionresult<person> get(string id)
        {
            var person = _repository.get(id);
            if (person == null)
            {
                return notfound();
            }

            return person;
        }

现在vs已经不会报错了,运行一下也可以正常工作。但仔细想想也很奇怪,为什么返回值类型改成了actionresult< person >就不报错了呢?明明返回值类型跟方法签名还是不一致啊?

深入actionresult< t >

接上面的问题,让我们看一下actionresult的内部:

看到这里就明白了原来actionresult< t >里面内置了2个implicit operator方法。implicit operator用于声明隐式类型转换。

public static implicit operator actionresult<tvalue>(actionresult result); 

表示actionresult类型可以转换为actionresult< tvalue >类型。

public static implicit operator actionresult<tvalue>(tvalue value)

表示tvalue类型可以转换为actionresult< tvalue >类型。

因为有了这2个方法,当actionresult或者tvalue类型往actionresult< t >赋值的时候会进行一次自动的类型转换。所以vs这里不会报错。

总结

  1. 大部分时候action的返回值可以使用actionresult/iactionresult
  2. 设计restful api的时候可以直接使用poco类作为返回值
  3. 如果要设计既支持poco类返回值或者actionresult类为返回值的action可以使用actionresult< t >作为返回值
  4. actionresult< t >之所以能够支持两种类型的返回值类型,是因为使用了implicit operator内置了2个隐式转换的方法

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网