网页API - 默认情况下渲染的Razor视图? [英] Web API - Rendering Razor view by default?

查看:98
本文介绍了网页API - 默认情况下渲染的Razor视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得网页API使用它返回的模式来呈现的Razor视图?而且,只有XML / JSON时接受头(或。扩展名)设置?这甚至可能?

How do I get Web API to render a Razor view using the model that it returns? And only XML/JSON when the accept headers (or .extension) is set? Is this even possible?

这太疯狂了,要求一组控制器渲染HTML,另一个用于JSON / XML,如果他们在同一个模型工作。

It seems crazy to require one set of controllers for rendering HTML and another for JSON/XML if they're working on the same models.

更新达雷尔 - 米勒已经写了<一个href=\"https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib.Formatting.RazorViewEngine/ViewEngineFormatter.cs\">ViewEngineFormatter对于剃刀可能做的伎俩,虽然还没有尝试过呢。

Update Darrel Miller has already written a ViewEngineFormatter for Razor which might do the trick, though haven't tried it yet.

推荐答案

我问起这个类似的问题在StackOverflow的过去,因为我想做同样的事情。不过,我终于结束了一个API区域和控制器集,一套标准MVC控制器的网站。

I asked a similar question about this in the past on StackOverflow, because I wanted to do the same thing. However, I eventually ended up with an "Api" area and set of controllers, and a standard set of MVC controllers for the website.

在事后这其实并不是一件坏事。我发现我倾向于在每一组控制器做不同的事情呢。我的看法不只是CRUD,但往往包含额外的上下文数据,因此返回特定于该页面视图模式是很好的。

In hindsight this actually wasn't a bad thing. I've found I tend to do different things in each set of controllers anyway. My views aren't just CRUD but tend to contain extra contextual data, so returning view models specific to that page is nice.

我想如果我坚持我的两者结合我可能已经结束了要么过于复杂的控制器或不如理想,因为它可能是一个用户体验的目标。因此,虽然这不是直接回答你的问题,在不能够做到这可能不是一件坏事我的经验。

I think if I had stuck to my goal of combining the two I might have ended up with either over-complicated controllers or a user experience that wasn't as optimal as it could have been. So while this isn't a direct answer to your question, in my experience not being able to do this might not be such a bad thing.

相反,我结束了一个丰富的建设者和命令,我大部分的控制器的委托来。通过这种方式,同时能够为API对网页做具体的事情,我可以重复使用的大部分控制逻辑的:

Instead I've ended up with a rich set of builders and commands that most of my controllers delegate to. That way I can reuse most of the controller logic while being able to do specific things for API versus the web:

<一个href=\"http://www.paulstovell.com/clean-aspnet-mvc-controllers\">http://www.paulstovell.com/clean-aspnet-mvc-controllers

这篇关于网页API - 默认情况下渲染的Razor视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆