2

I was reading requirements for an online project. it was about asp.net web API. The requirement is one api method /ui/echo/, which echoes any string passed to it as a parameter, which when called, would respond with an UI HTML partial <b>Put echo string here</b>.

I read about RazorMediaTypeFormatter over the internet. Is there any other way web api can return partial views?

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • 1
    What is the design constraint forcing you to use ApiController to return partial views? Why not use a standard site Controller which was made for returning partial views. ApiControllers are for RESTful API's that return data in xml or json formats. – Kevin Junghans Aug 07 '12 at 20:01
  • This could be dangerous from a security perspective unless you're encoding the parameters using something like AntiXSS. Otherwise a malicious user can send in Javascript, IFRAME, object tags, etc., which would be injected into HTML that's presented to a user. – Jon Galloway Aug 07 '12 at 20:14
  • Its a sample requirement. working app would definitely fetch data from some other source. The question is returning a partial from web api. – Muhammad Adeel Zahid Aug 08 '12 at 05:36
  • So more specifically you would like to take a partial view written with Razor and return the rendered HTML as a result of a request to a REST API. Is that correct? So is the question how to get the resulting HTML from a Razor view, or is it how to return HTML in a REST API, or both? – Kevin Junghans Aug 08 '12 at 12:34
  • Yes its correct. The question is how to get the result from razor view using asp.net web api. Is the TypeFormatter only option or is there anything else that can be done? More specifically with the ability to specify the view name. I know its weird, for such requirement I would go to normal mvc controller but as I said in question, its not my requirement but from an online job on odesk – Muhammad Adeel Zahid Aug 08 '12 at 18:04
  • @KevinJunghans is spot on here in my opinion, for others who are thinking about doing this, I would ask myself why I "NEED" to do it. Returning a view is a misuse of the API Controller – Pure Function Sep 13 '12 at 22:50
  • Yes, he is right. I just read in requirements of an online job – Muhammad Adeel Zahid Sep 14 '12 at 05:14
  • @MuhammadAdeelZahid did you get the job? – Simon_Weaver Dec 15 '12 at 22:02
  • I did not try but believed that following the tutorials should have gotten me there – Muhammad Adeel Zahid Dec 16 '12 at 15:29

1 Answers1

4

I would look to the open source project MvcMailer to see how you could render the Razor View into a string that you could then be put into your response to a REST request. MvcMailer uses Razor Views to render the body of an email as HTML. Since ASP.NET Web API is part of MVC 4 it should integrate well. I have used MvcMailer in MVC 4 projects and it works fine. Look at StringResult.ExecuteResult. That appears to be the meat of the code for rendering a View as a string. Never tried to return HTML in a REST result, and it is usually discouraged. According to this QA you will need to encode the HTML before sending the result back as JSON. There is some good discussion on how to return HTML results here.

Community
  • 1
  • 1
Kevin Junghans
  • 17,475
  • 4
  • 45
  • 62
  • For returning "raw" content (html, javascript, etc) given a string see [my suggestion](http://stackoverflow.com/questions/14491518/dynamic-javascript-returned-by-webapi/19036655#19036655) and [this discussion](http://stackoverflow.com/questions/11581697/is-there-a-way-to-force-asp-net-web-api-to-return-plain-text/11582207#comment28130776_11582207) – drzaus Sep 26 '13 at 19:37