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 }
                    );
                }
            }
        }

また、私の/apiurlは機能しません

4

1 に答える 1

1

Web APIから継承する必要がありApiControllerます(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

[Serializable]最後に、Web APIの場合、属性は必要ありません。

于 2013-03-01T00:58:18.320 に答える