views:

314

answers:

3

I am playing around with ASP.NET MVC for the first time, so I apologize in advance if this sounds academic.

I have created a simple content management system using ASP.NET MVC. The url to retrieve a list of content, in this case, announcements, looks like:

http://www.mydomain.com/announcements/list/10

This will return the top ten most recent announcements.

My questions are as follows:

  1. Is it possible for any website to consume this service? Or would I also have to expose it using something like WCF?

  2. What are some examples, of how to consume this service to display this data on another website? I'm primarily programming in the .NET world, but I'm thinking if I could consume the service using javascript, or do something with Json, it could really work for any technology.

I am looking to dynamically generate something like the following output:

<div class="announcement">
    <h1>Title</h1>
    <h2>Posted Date</h3>
    <p>Teaser</p>
    <a href="www.someotherdomain.com">More</a>
</div>
+1  A: 

There is nothing to stop another client just scraping that particular page and parsing through your HTML.

However you would probably want another view using the same controller that generates the data that doesnt contain excess formatting HTML etc. Maybe look at using a well known format such as RSS?

You can return the result as JSON using something like below:

public JsonResult GetResults() { return Json(new { message = "SUCCESS" }); }

I think I would offer a view which contains the items as xml and another that returns JSON that way you have the best of both worlds.

I have a small post about how to call and return something using MVC, JQuery and JSON below: http://www.simpleisbest.co.uk/Home/Index/MVC?tagId=1

alexmac
+1  A: 

Your ROUTE is perfectly fine and consumable by anyone. The trick is to how you want to expose your data for that route. You said XML. sure. You can even do JSon or Html or just plain ole text.

The trick would be in your controller Method and the view result object.

Here's the list of main view results :-

  • ActionResult
  • ContentResult
  • EmptyResult
  • JsonResult
  • RedirectResult

eg.

public <ContentResult> AnnouncmentIndex(int numberOfAnnouncements)
{
   // Generate your Xml dynamically.
   string xml = "<div class=\"announcement\"><h1>Title</h1><h2>Posted Date</h3><p>Teaser</p><a href="www.someotherdomain.com">More</a></div>"


   Response.ContentType = "application/xml"; // For extra bonus points!

   return Content(xml);
}
Pure.Krome
A: 

For now ... is it possible to return an Html representation and display it in a webpage? Is this possible using just Javascript?

mattruma