将 ASP.NET MVC 路由 URL 添加到视图模型对象(用于 JSON)

发布于 2025-01-08 11:51:54 字数 1236 浏览 1 评论 0原文

我正在编写一个应用程序,它使用 ASP.NET MVC 和 JSON.NET 作为服务器,将 JSON 发送到客户端,该客户端由 Knockout 读取,然后使用数据绑定显示。这一切都工作得很好,除了一个问题:

我有一个类 Customer ,它用于生成 ReviewAuthorViewModel 类 - 后者专门用于 JSON 序列化并删除不必要的在客户端上,我想呈现一个指向客户个人资料页面的链接,其中 URL 来自定义的 MVC 路由“user/{username}”。因为我是通过 JSON 发送的,所以我没有可以调用 Url.Action.cshtml 页面。

问题是:对于任意对象,如何在没有 .cshtml 视图的情况下最有效/优雅地获取包含某些数据的操作的 URL?

我更喜欢在每个操作中不需要额外代码的解决方案,但这可能是除了在 JavaScript 中重新创建客户端路由表之外的唯一选择。以下是我已经尝试过的事情,以及每件事都不满意的地方。

解决方案尝试 1

调用 ReviewAuthorViewModel 类中的 UrlHelper.Action 方法。但是,UrlHelper 需要请求上下文。为了分离关注点,我不希望我的视图模型依赖于 System.Web.Routing,也不希望它需要请求上下文才能运行。

解决方案尝试 2

创建一个包含 Controller、Action 和 Data 成员的类 RouteInformation,以及一个包含两个属性(类型为 get )的接口 IUrlViewModel code>RouteInformation 和一个名为 Url 的获取/设置字符串。然后,需要链接的视图模型实现此接口,控制器查找 IUrlViewModel 类型的视图模型,并使用视图模型的 RouteInformation< 中的信息运行 UrlHelper.Action /code> 实例,将结果存储在 Url 属性中。

这个有效,但如果没有反思,我不知道如何在其他视图模型中找到实现 IUrlViewModel 的视图模型,感觉非常混乱。

I'm writing an application which uses ASP.NET MVC with JSON.NET as the server, sending JSON to the client which is read by Knockout and then displayed with data-bindings. This is all working wonderfully, except for one problem:

I have a class Customer which is used to generate a ReviewAuthorViewModel class - the latter is meant specifically for JSON serialization and removes unnecessary fields, circular references, etc. On the client, I want to render a link to the Customer's profile page, with the URL coming from a defined MVC route "user/{username}". Because I'm sending this via JSON, I don't have a .cshtml page in which I can call Url.Action.

The question is: For an arbitrary object, how do I most efficiently/elegantly get a URL for an action with some data, without a .cshtml view?

I'd prefer a solution that doesn't require extra code in each action, but that may be the only choice apart from recreating the routing tables client-side in JavaScript. Below are the things I've already tried, and what was unsatisfactory with each.

Solution Attempt 1

Call the UrlHelper.Action method in the ReviewAuthorViewModel class. However, the UrlHelper requires a request context. For the sake of separation of concerns, I don't want my view model to have a dependency on System.Web.Routing, nor do I want it to need a request context to function.

Solution Attempt 2

Create a class RouteInformation with members Controller, Action, and Data, and an interface IUrlViewModel with two properties, a get of type RouteInformation and a get/set string named Url. The view models needing links then implement this interface, and controllers look for view models of type IUrlViewModel and run UrlHelper.Action with the information from the view model's RouteInformation instance, storing the result in the Url property.

This one works but without reflection I don't know how to find view models implementing IUrlViewModel within other view models, and it feels very kludgy.

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

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

发布评论

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

评论(1

通知家属抬走 2025-01-15 11:51:54

解决方案尝试 1 是可行的解决方案。在 asp.net-mvc 中,视图模型是表示层的一部分,专为与视图一起使用而设计。您不应该担心视图模型依赖于 ASP.NET 特定的东西。事实上,它们应该是耦合的,因为它们的设计应该最大限度地简化 Web 服务器和 Web 客户端之间的视图生成和数据交换。创建单独的视图模型而不是直接为客户端使用 Customer 类是一个很好的解决方案。如果 Customer 依赖于 Routing,那就不行了。

事实上,您可以在控制器中设置该属性 -

    [HttpGet]
    public ActionResult Get()
    {
        var viewModel = new ReviewAuthorViewModel();
        viewModel.ProfilePageUrl = Url.Action("Index", "Profile");

        // return viewModel;
    }

Solution Attempt 1 is the OK solution. In asp.net-mvc, view models are part of presentation layer, designed specifically for use with views. You should not worry about having view model depend on asp.net specific things. In fact, they should be coupled, as they should be designed to maximize simplicity of view generation and data exchange between web server and web client. And it's good solution to create separate view model and not use Customer class directly for clients. It wouldn't have been OK if Customer was dependent on Routing.

In fact, you could set that property in controller -

    [HttpGet]
    public ActionResult Get()
    {
        var viewModel = new ReviewAuthorViewModel();
        viewModel.ProfilePageUrl = Url.Action("Index", "Profile");

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