且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

网页API - 默认情况下渲染的Razor视图?

更新时间:2021-11-24 00:43:26

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

I asked a similar question about this in the past on ***, 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:

http://www.paulstovell.com/clean-aspnet-mvc-controllers