嗨,我在 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 网址也不起作用