0

嗨,我在 web-api 中构建了一个测试应用程序,但我返回的对象没有被序列化

控制器

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            using System.Web.Mvc;
            using Thoughts.Models;
            using Newtonsoft.Json;
            namespace Thoughts.Controllers
            {
            public class TagsController : Controller
            {
                //
                // GET: /Tags/

                public ActionResult Index()
                {
                    return View();
                }


                public Message test(){
                    Message msg = new Message();
                    msg.Content = "jo";
                    return msg;
                }
            }
            }

消息模型

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;

            namespace Thoughts.Models
            {
            [Serializable]
            public class Message //: NamedObject
            {
                string content;

                public string Content
                {
                    get { return content; }
                    set { content = value; }
                }
                /*public NamedObject User;
                public string timestamp;
                public List<NamedObject> tags;
                public Guid communityid;
                public List<Message> comments;
                public int commentCount;
                public string parent;*/
            }
            }

路由器配置

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Web;
            using System.Web.Mvc;
            using System.Web.Routing;

            namespace HelloWebAPI
            {
                public class RouteConfig
                {
                    public static void RegisterRoutes(RouteCollection routes)
                    {
                        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                        routes.MapRoute(
                            name: "Default",
                            url: "{controller}/{action}/{id}",
                            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                        );
                    }
                }
            }

WebApiConfig

            using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web.Http;

        namespace Thoughts
        {
            public static class WebApiConfig
            {
                public static void Register(HttpConfiguration config)
                {
                    config.Routes.MapHttpRoute(
                        name: "DefaultApi",
                        routeTemplate: "api/{controller}/{action}/{id}",
                        defaults: new { id = RouteParameter.Optional }
                    );
                }
            }
        }

我的 /api 网址也不起作用

4

1 回答 1

1

您需要从ApiControllerWeb API 继承(Web API 控制器与 MVC 控制器是分开的):

public class TagsController : ApiController {

        [System.Web.Http.HttpGet]
        public Message test(){
                Message msg = new Message();
                msg.Content = "jo";
                return msg;
        }

 }

由于您的 HTTP 路由是api/{controller}/{action}/{id}

您现在可以调用它:/api/tags/test

最后,对于 Web API,您不需要[Serializable]属性。

于 2013-03-01T00:58:18.320 回答