Web API - Rendering Razor view by default?

后端 未结 5 962
执念已碎
执念已碎 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:29

    I am looking for something similar to this, but not entirely. Through scouring the web, I found a couple posts by Fredrik Normen. He writes about this exact problem space and actually identifies a third-party solution in the second post listed. Basically, the solution involves creating a custom MediaTypeFormatter that knows how to handle views using the Razor engine provided by Microsoft (through the use of a third-party library).

    • Using Razor engine together with Asp.Net Web API to create a Hypermedia API
    • Using Razor together with ASP.NET Web API
    • ASP.Net Web API and using Razor the next step

    Hopefully Microsoft will implement something soon in Web API as Hypermedia seems to be gaining traction.

    Hope this helps!

提交回复
热议问题