How to use data from ASP.NET MVC from another site?

This is my first time with ASP.NET MVC, so I apologize in advance if this sounds academic.

I have created a simple content management system using ASP.NET MVC. The URL to get the list of content, in this case, ads, looks like this:

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

      

This will return the top ten most recent ads.

My questions are:

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

  • What are some examples of using this service to display this data on another website? I am primarily programming in the .NET world, but I think if I could use the service with javascript or do something with Json, it really could work for any technology.

I want 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>

      


For now ... is it possible to get the Html view back and display it on the web page? Is Javascript only possible?

0


source to share


2 answers


There is nothing to stop another customer simply by scraping that particular page and parsing your HTML.

However, you will probably need a different view using the same controller that generates data that does not contain redundant HTML formatting, etc. Maybe look at using a well-known format like RSS?

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



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

      

I think I propose a view that contains elements as xml and another that returns JSON in a way that gives you the best of both worlds.

I have a little post on how to call and return something using MVC, JQuery and JSON here .

+1


source


Your ROUTE is great and can be consumed by anyone. The trick is how you want to provide your data for this route. You said XML. sure. You can even do JSon or Html or just plain ole text.

The trick will be in your controller method and view result object.

Here is a list of the main 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);
}

      

+1


source







All Articles