如何获得“服务”? ASP.NET MVC 中的页面?

发布于 2024-10-14 16:16:55 字数 264 浏览 1 评论 0原文

MVC 新手:

我已经或多或少地解决了 MVC 的页面导航方面的问题。但假设我不想导航到视图,而是想从网站获得响应,例如通过向 http://mysite.com/Services/GetFoo/123 我想发出一个数据库请求来选择 ID 为 123 的 Foo 对象并返回它序列化为 XML。

你是怎么做到的?

MVC newbie here:

I've more or less worked out the page navigation aspect of MVC. But let's say I don't want to navigate to a View, but rather I want to get a response out of the web site, e.g. by sending a request to http://mysite.com/Services/GetFoo/123 I want to make a database request to select a Foo object with ID 123 and return it serialized as XML.

How do you do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲琵琶半遮面シ 2024-10-21 16:16:55

我会编写一个自定义操作结果:

public class XmlResult : ActionResult
{
    private readonly object _data;
    public XmlResult(object data)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }
        _data = data;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        // You could use any XML serializer that fits your needs
        // In this example I use XmlSerializer
        var serializer = new XmlSerializer(_data.GetType());
        var response = context.HttpContext.Response;
        response.ContentType = "text/xml";
        serializer.Serialize(response.OutputStream, _data);
    }
}

然后在我的控制器中:

public ActionResult GetFoo(int id)
{
    FooModel foo = _repository.GetFoo(id);
    return new XmlResult(foo);
}

如果这个 return new XmlResult(foo); 在你看来很难看,你可以有一个扩展方法:

public static class ControllersExtension
{
    public static ActionResult Xml(this ControllerBase controller, object data)
    {
        return new XmlResult(data);
    }
}

然后:

public ActionResult GetFoo(int id)
{
    FooModel foo = _repository.GetFoo(id);
    return this.Xml(foo);
}

I would write a custom action result:

public class XmlResult : ActionResult
{
    private readonly object _data;
    public XmlResult(object data)
    {
        if (data == null)
        {
            throw new ArgumentNullException("data");
        }
        _data = data;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        // You could use any XML serializer that fits your needs
        // In this example I use XmlSerializer
        var serializer = new XmlSerializer(_data.GetType());
        var response = context.HttpContext.Response;
        response.ContentType = "text/xml";
        serializer.Serialize(response.OutputStream, _data);
    }
}

and then in my controller:

public ActionResult GetFoo(int id)
{
    FooModel foo = _repository.GetFoo(id);
    return new XmlResult(foo);
}

And if this return new XmlResult(foo); feels ugly to your eyes, you could have an extension method:

public static class ControllersExtension
{
    public static ActionResult Xml(this ControllerBase controller, object data)
    {
        return new XmlResult(data);
    }
}

and then:

public ActionResult GetFoo(int id)
{
    FooModel foo = _repository.GetFoo(id);
    return this.Xml(foo);
}
夜吻♂芭芘 2024-10-21 16:16:55

听起来您想创建一个 REST API。

看看午睡将完成所有繁重的工作。

或者,您可以编写一个操作方法,该方法返回呈现为 XML 而不是 HTML 的视图。

像这样的东西:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyModel>" ContentType="text/xml" %>
<%= SerializationHelper.SerializeAsXml(Model) %>

Sounds like you want to create a REST API.

Have a look at Siesta which will do all the heavy lifting.

Alternatively you could write an action method which returns a view which renders as XML rather than HTML.

Something like:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyModel>" ContentType="text/xml" %>
<%= SerializationHelper.SerializeAsXml(Model) %>
阳光的暖冬 2024-10-21 16:16:55

如果您可以接受 JSON 结果,则以下操作应该可行:

public class ServicesController : Controller
{
    public ActionResult GetFoo(int id)
    {
        var dbResult = SomeDbUtil.GetFoo(id);
        return this.Json(dbResult);
    }
}

这将为您提供相当基本的 JSON 查询结果。但是,如果您希望您的服务是可发现的 SOAP XML 服务等,那么设置另一个项目/网站来充当 Web 服务对我来说似乎是更好的主意。

If you could live with a JSON result, the following should work:

public class ServicesController : Controller
{
    public ActionResult GetFoo(int id)
    {
        var dbResult = SomeDbUtil.GetFoo(id);
        return this.Json(dbResult);
    }
}

This would give you pretty a basic JSON query result. However, if you want your services to be discoverable SOAP XML services etc., setting up another project/website that acts as the web service would seem to be the better idea to me.

╰◇生如夏花灿烂 2024-10-21 16:16:55

您可能可以在这里找到问题的答案:

请参阅从控制器的操作中返回 XML 作为 ActionResult?

You can probably find an answer to your question here:

See Return XML from a controller's action in as an ActionResult?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文