Web API - Rendering Razor view by default?

后端 未结 5 951
执念已碎
执念已碎 2021-02-13 16:52

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?

It

5条回答
  •  独厮守ぢ
    2021-02-13 17:32

    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.

    Web API is exactly what it is called - a technology for creating API's.

    If you are creating an ASP.NET MVC application and want to return some JSON for your own purpose then you don't need content negotiation etc. therefore you don't need Web API (just use plain old good JsonResult).

    If you want to create a reusable API than Web API is what you need but your client application should consume it in the same way as everybody else.

    Web API isn't meant to be a "hammer" for "nailing" all non-HTML requests - use it when you need it.

提交回复
热议问题