A04 - Web Services, Screen Scraping, RSS In this
assignment you will use web services to publish a search of the music store database.
You will then consume it and display the results in an MVC view. You will also scrape WWU's news page, and read a RSS feed.
- MusicSearchApi/SearchMusic/love -
This exercise creates a web service
that searches the music store database and returns the results via JSON.
- Database: Create a Sql Server music store database as in MIS 324 A07 step 1 a. You need only tblDescription, not tblStylesAmazon. Add a connection string to the database in web.config.
- Model: Add a music item model. Copy your music model from MIS 324.
- DataRepository: Create a new DataRepository named MusicRepository (RetailRepository from A02 or CRUDRepository from MIS 324 A05 part 1a would be good templates. Add a method named SearchMusic that returns List<MusicItemModel> . Write a sql statement that uses SQL wildcards to search title and artists fields. The answer by Gravell show the syntax for using wildcards with Dapper.
- Controller: Create a new empty controller named MusicSearchApiController. Add a action method named SearchMusic(string id). This action method will retrieve a List<> of data from MusicRepository and format it as JSON. The syntax is simple:
MusicRepository musicRepository = new MusicRepository();
public JsonResult SearchMusic(string id)
return Json(musicRepository.SearchMusic(id), JsonRequestBehavior.AllowGet);
- MusicSearchApi/Consume/love- This exercise consumes the music search API service created in part 1 and displays it in the default table view created by VS.
- Namespaces: Since you have are consuming the API within an MVC project
(rather than a Web API project) you will need to add a few additional namespaces
to your controller. Add the following to the MusicSearchApiController:
- References: Right-click on "References," select Add
Reference, and add "System.Web.Extensions." You may also need to add "System.Web.Http."
- Controller: Create a new action method named ConsumeApi and
modify WebServiceReadJson to read the
web service you created in the previous exercise.
- View: Use VS to add a List view (model is MusicModelItem).
- MusicSearchApi/Consume2 - This exercise creates an improved view that contains a search box and formatted results.
- Controller: Copy and paste Consume and name the copy Consume2. Modify
it to accept a parameter named query of type string. Edit the URL to search for "query" rather than a static query. Also modify the method to return an empty
view if the parameter is null.
- View: Add a search box (you can copy from the sample). Also wrap the foreach loop with a
test to see if the model contains data.
The foreach loop will throw an exception if the model is empty.
@if(Model != null)
- Add a view bag that displays the count from the controller. Just before the return statement in the controller add:
ViewBag.count = model.Count + " titles contain " + query;
- Replace the table layout with properly formatted e-commerce layout
(title, artist, image, description, and Amazon link). See MIS 324 A07 part 1a for a refresher on the layout. Use Razor syntax to truncate the editorial review:
@Html.Raw(item.Review.Substring(0, Math.Min(150, item.Review.Length)))
- ScreenScrape/wwuNews - This exercise scrapes WWU's popular new headlines.
- Add the code from the handout ScreenScrape.doc
to your project. It should produce this output.
- Controller: Add a second action method named WwuNews. Copy and paste the code from
the index action method and remove the code that deals with the form since you will be hard-coding the targetURL and regExPattern
into the script. Add the WWU
news page as targetUrl.
- View: copy the index view and remove the form.
- RegEx pattern. Examine the html in the news page and try to identify a distinctive pattern that captures the desired data. It can be useful to include CSS style tags so that you can reuse the styles in your page. Write a regular expression pattern. The MIS 424 website has a regular expression tester that may be useful. Paste in the html from the target page and experiment with different patterns. Tip: if you pattern includes double-quotes you can escape them with a back-slash.
- The content you desire may not have a distinctive pattern to distinguish it from content that you do not desire. In these cases you may need to remove undesired text using C# methods such as indexOf(), substring(), and the length property.
RssReader/index - RSS stands for "really simple syndication."
RSS is a standardized XML format which is used to distribute web content.
Atom are both XML format standards. In this exercise you will
read the Seattle Times RSS news feed and display it in a MVC view.
- Model: Look at the Seattle Times RSS
feeds. Click on "all articles" to view the feed.
The fields that your will display are Title, PubDate, Link, and Description.
Build a model named RssModel with these four properties.
- Controller: Create a new empty controller named RssController. The article
EASY WAY TO READ AN RSS FEED shows how to read an RSS feed into a List<>. Copy the code from "var reader" to "return" and paste it into the index method of the controller. Add a reference to System.ServiceModel to your project. Add using statements for System.Xml and System.ServiceModel.Syndication.
- Modify the code to use the URL of the Seattle Times news feed.
Replace the BlogModel with your RssModel. Modify the
code in the foreach loop to copy the four fields from the feed to your model:
RssModel article = new RssModel();
article.Title = i.Title.Text;
article.PubDate = i.Summary.Text;
article.Link = i.Links.Uri.OriginalString;
article.Description = i.Summary.Text;
- Create a view using template List and Model RSSModel. DataContext textbox should be empty.
- Improve the layout to display titles (with links to the article), description, and pubDate. You may
copy the formatting from the sample. The image tag is located inside the description so
use @Html.Raw(item.Description) to display the description. The images are very
large so the sample site includes a snippet of CSS to reduce their size.
- Display an article count. Add a ViewBag object to the controller to get the count of items in the List. Add a corresponding @ViewBag.count statement in the view to display the count.
Submission instructions: Submit assignments via
the Canvas course management system.
Submit the full URL for each exercise in the assignment, listing the URLs
in the same order that they are listed in the assignment. To minimize typos in URLs
it is strongly recommended
that you copy the URLs from the address bar of the browser
rather than trying to type them. Incorrect URLs
will not be graded and no credit will be given.
When pages are connected via navigation it is only
necessary to submit the URL of the first page.