I am still learning ASP.NET/MVC and following along a tutorial by Matt Blagden (You Tube) on building a Blog..
After making the Post Controller I am getting an error in a snippet of code that handles adding a Tag to a Post if it is a new Post. My thought is the method AddToPosts should have been generated when I added a new Entity Data Model.
The error is:
blog.Models.BlogModel does not contain a definition for 'AddToPosts' and no extension method 'AddToPosts'. PostsController.cs accepting a first argument of type 'blog.Models.BlogModel' could be found (are you missing a using directive or an assembly reference?)
The portion of code that is giving me the error is:
if (!id.HasValue)
{
model.AddToPosts(post);
}
Here is the entire Post Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using blog.Models;
using System.Data.EntityModel;
using System.Text;
namespace blog.Controllers
{
public class PostsController : Controller
{
// Access Model
private BlogModel model = new BlogModel();
public ActionResult Index()
{
return View();
}
// A way to input sample data into database
[ValidateInput(false)]
public ActionResult Update(int? id, string title, string body, etc.....)
{
//IF not an admin redirect back to index
if (!IsAdmin)
{
return RedirectToAction("Index");
}
// Get Post
Post post = GetPost(id);
// Populate simple properties
post.Title = title;
post.Body = body;
post.DateTime = dateTime;
post.Tags.Clear(); // first clear tag list
//ensure input sequence of tag names is not null
tags = tags ?? string.Empty;
//Split the sequence into a list of tag names
string[] tagNames = tags.Split(new char[] { ' ' }, etc...
//Get or create each tag that was named
foreach (string tagName in tagNames)
{
//Add the tag
post.Tags.Add(GetTag(tagName));
}
if (!id.HasValue)
{
model.AddToPosts(post);
}
model.SaveChanges();
return RedirectToAction("Details", new { id = post.ID });
}
// PROMPTS USER TO INPUT DATA FOR DATABASE
public ActionResult Edit(int? id)
{
Post post = GetPost(id);
//ACCUMULATES LIST OF CURRENT TAGNAMES
StringBuilder tagList = new StringBuilder();
foreach (Tag tag in post.Tags)
{
//Append each tagname
tagList.AppendFormat("{0} ", tag.Name);
}
//Gives the tagList to the view
ViewBag.Tags = tagList.ToString();
return View(post);
}
private Tag GetTag(string tagName)
{ // if tag is set then Get the Tag, if not create a new one (Just like GetPost)
return model.Tags.Where(x => x.Name == tagName).FirstOrDefault() ?? etc....
}
private Post GetPost(int? id)
{
// IF id is set then Get the Post, if not make a new one..
return id.HasValue ? model.Posts.Where(x => x.ID == id).First() : etc.....
// TODO: don't just return true
public bool IsAdmin
/* READS SESSION STATE */
{
get { return true; /*{ return Session["IsAdmin"] != null && etc...
}
}
}
If "AddToPosts" coming from static class, try adding using statement of namespace where the Add post class exists.
I have a finalist page that displays a list of finalists, that are clickable into a single-view page. I have the XML document being passed into a model and am spilling that data onto the main finalists page, but I can't seem to figure out how to grab the clicked finalist's id and display only that finalist on the single-view page.
Any help is appreciated, here is my controller right now:
I am trying to pass the newly created model in to the singleView class, but I'm not sure how to filter it to know which finalist was clicked on, and which finalist to display on the single-view page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using ProjectX_Awards.Models;
namespace ProjectX_Awards.Controllers
{
public class FinalistsController : Controller
{
//
// GET: /Finalists/
public ActionResult Index()
{
var doc = XElement.Load(HttpContext.Server.MapPath("~/finalist.xml"));
var finalists = doc.Descendants("finalist").Select(f => new Models.Finalist()
{
Id = (int)f.Attribute("id"),
Name = f.Descendants("name").First().Value,
Description = f.Descendants("description").First().Value,
Link = f.Descendants("webLink").First().Value,
Photo = f.Descendants("photoUrl").First().Value
});
return View(finalists);
}
public ActionResult SingleView(Finalist model)
{
var singleFinalist = model;
return View(singleFinalist);
}
}
}
If you want to pass a complete model, you need to do a POST to that action-method. The easiest way is to make sure you post all the values in a form-element to the specified action. However, the best thing would be to pass an Id to your SingleView-method. This allows you to do a get to that page, instead of having to post a complete object:
public ActionResult SingleView(int id)
{
var singleFinalist = model;
var doc = XElement.Load(HttpContext.Server.MapPath("~/finalist.xml"));
var finalist = doc.Descendants("finalist").Where(f => (int)f.Attribute("id") == id)
.Select(f => new Models.Finalist()
{
Id = (int)f.Attribute("id"),
Name = f.Descendants("name").First().Value,
Description = f.Descendants("description").First().Value,
Link = f.Descendants("webLink").First().Value,
Photo = f.Descendants("photoUrl").First().Value
})
.FirstOrDefault;
return View(finalist);
}
Then in your finalists-page you can just emit an a-tag like this:
#foreach(var finalist in Model)
{
Detail
// or
#Html.ActionLink("SingleView", "YourController", new { id = finalist.id })
}
EDIT Adding a simple caching method, so the XML is not reloaded everytime:
public ActionResult Index()
{
return View(GetAllFinalists());
}
public ActionResult SingleView(int id)
{
var doc = XElement.Load(HttpContext.Server.MapPath("~/finalist.xml"));
var finalist = GetAllFinalists().Where(f => f.Id == id)
.FirstOrDefault;
return View(finalist);
}
private IEnumerable<Models.Finalist> GetAllFinalists()
{
if (HttpContext.Current.Application["finalists"] == null)
{
var doc = XElement.Load(HttpContext.Server.MapPath("~/finalist.xml"));
HttpContext.Current.Application["finalists"] = doc.Descendants("finalist")
.Select(f => new Models.Finalist()
{
Id = (int)f.Attribute("id"),
Name = f.Descendants("name").First().Value,
Description = f.Descendants("description").First().Value,
Link = f.Descendants("webLink").First().Value,
Photo = f.Descendants("photoUrl").First().Value
});
}
return (IEnumerable<Models.Finalist>)HttpContext.Current.Application["finalists"];
}
I'm using ASPxGridView and updating with data from DataTable which, works fine.
But when I'm trying to use edit, update, delete, paging, sorting functions I get in trouble.
The buttons are displayed but the function doesn't work when I click them.
Here is my Sample.aspx file:
[ASPx]
<dx:aspxgridview ID="grid" ClientInstanceName="grid" runat="server"
OnDataBinding="grid_DataBinding" Width="100%" EnableCallBacks="false">
<Settings ShowTitlePanel="True" />
<SettingsText Title="Tabla" />
<SettingsPager PageSize ="10" ShowSeparators="true" PageSizeItemSettings-Items="20" >
<PageSizeItemSettings Visible="true" />
</SettingsPager>
</dx:aspxgridview>
The code behind Sample.aspx.cs :
[C#]
using System;
using System.Data;
using System.Web.Mvc;
using DataConnector;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
namespace Sample.Views.Actions
{
public partial class Sample: ViewPage
{
private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
grid.DataBind();
grid.RowValidating += new ASPxDataValidationEventHandler(grid_RowValidating);
if (!this.IsPostBack)
{
GridViewCommandColumn c = new GridViewCommandColumn();
grid.Columns.Add(c);
c.Caption = "Operations";
c.EditButton.Visible = true;
c.UpdateButton.Visible = true;
c.NewButton.Visible = true;
c.DeleteButton.Visible = true;
c.CancelButton.Visible = true;
}
}
DataTable BindGrid()
{
DataConnection DM = new DataConnection();
if (DM.login("ADMIN", "PASS"))
dt = DM.getDataSet("SELECT * FROM Table_Name");
grid.DataSource = dt;
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
return dt;
}
protected void grid_DataBinding(object sender, EventArgs e)
{
// Assign the data source in grid_DataBinding
BindGrid();
}
protected void grid_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
{
int id = (int)e.OldValues["id"];
DataRow dr = dt.Rows.Find(id);
dr[0] = e.NewValues[""];
dr[1] = e.NewValues[""];
dr[2] = e.NewValues[""];
dr[3] = e.NewValues[""];
dr[4] = e.NewValues[""];
ASPxGridView g = sender as ASPxGridView;
UpdateData(g);
g.CancelEdit();
e.Cancel = true;
}
void grid_RowValidating(object sender, ASPxDataValidationEventArgs e)
{
int id = (int)e.NewValues["id"];
if ((!e.OldValues.Contains("id") || ((int)e.OldValues["id"] != id))
&& (dt.Rows.Find(id) != null))
{
ASPxGridView grid = sender as ASPxGridView;
e.Errors[grid.Columns["id"]] = String.Format("Column 'Id' is constrained to be unique. Value '{0}' is already present.", id);
}
}
private void UpdateData(ASPxGridView g)
{
g.DataBind();
}
}}
The Sample is called on the controller page:
[C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using DataConnector;
namespace Sample.Controllers
{
public class ActionsController : Controller
{
public ActionResult Sample()
{
return View();
}
}
}
The data displayed on the Grid.
I created a new solution where is only the Sample.aspx and Sample.aspx.cs and I'm using without MVC and the functions are working. The difference is "public partial class Sample: System.Web.UI.Page ".
If I try without System.Web.UI.Page on my mentioned code it gives me the following error:
The view must derive from ViewPage, ViewPage<TModel>, ViewuserControl,or ViewuserControl<TModel>.
What am I doing wrong? Why does not the function of the ASPxGridView working?
I'm sorry in advance if it is poorly drafted but not my mother tongue is English, and I am a new programmer :)
The ASPxGridView is a WebForms control and cannot be used within ASP.NET MVC. Instead you should use the DevExpress MVC extensions. Specifically, the DevExpress MVC GridView extension
Watch my "Getting started with DevExpress MVC video" to learn more.
How can dynamic breadcrumbs be achieved with ASP.net MVC?
If you are curious about what breadcrumbs are:
What are breadcrumbs? Well, if you have ever browsed an online store or read posts in a forum, you have likely encountered breadcrumbs. They provide an easy way to see where you are on a site. Sites like Craigslist use breadcrumbs to describe the user's location. Above the listings on each page is something that looks like this:
s.f. bayarea craigslist > city of san francisco > bicycles
EDIT
I realize what is possible with the SiteMapProvider. I am also aware of the providers out there on the net that will let you map sitenodes to controllers and actions.
But, what about when you want a breadcrumb's text to match some dynamic value, like this:
Home > Products > Cars > Toyota
Home > Products > Cars > Chevy
Home > Products > Execution Equipment > Electric Chair
Home > Products > Execution Equipment > Gallows
... where the product categories and the products are records from a database. Some links should be defined statically (Home for sure).
I am trying to figure out how to do this, but I'm sure someone has already done this with ASP.net MVC.
Sitemap's are definitely one way to go... alternatively, you can write one yourself! (of course as long as standard MVC rules are followed)... I just wrote one, I figured I would share here.
#Html.ActionLink("Home", "Index", "Home")
#if(ViewContext.RouteData.Values["controller"].ToString() != "Home") {
#:> #Html.ActionLink(ViewContext.RouteData.Values["controller"].ToString(), "Index", ViewContext.RouteData.Values["controller"].ToString())
}
#if(ViewContext.RouteData.Values["action"].ToString() != "Index"){
#:> #Html.ActionLink(ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["action"].ToString(), ViewContext.RouteData.Values["controller"].ToString())
}
Hopefully someone will find this helpful, this is exactly what I was looking for when I searched SO for MVC breadcrumbs.
ASP.NET 5 (aka ASP.NET Core), MVC Core Solution
In ASP.NET Core, things are further optimized as we don't need to stringify the markup in the extension method.
In ~/Extesions/HtmlExtensions.cs:
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace YourProjectNamespace.Extensions
{
public static class HtmlExtensions
{
private static readonly HtmlContentBuilder _emptyBuilder = new HtmlContentBuilder();
public static IHtmlContent BuildBreadcrumbNavigation(this IHtmlHelper helper)
{
if (helper.ViewContext.RouteData.Values["controller"].ToString() == "Home" ||
helper.ViewContext.RouteData.Values["controller"].ToString() == "Account")
{
return _emptyBuilder;
}
string controllerName = helper.ViewContext.RouteData.Values["controller"].ToString();
string actionName = helper.ViewContext.RouteData.Values["action"].ToString();
var breadcrumb = new HtmlContentBuilder()
.AppendHtml("<ol class='breadcrumb'><li>")
.AppendHtml(helper.ActionLink("Home", "Index", "Home"))
.AppendHtml("</li><li>")
.AppendHtml(helper.ActionLink(controllerName.Titleize(),
"Index", controllerName))
.AppendHtml("</li>");
if (helper.ViewContext.RouteData.Values["action"].ToString() != "Index")
{
breadcrumb.AppendHtml("<li>")
.AppendHtml(helper.ActionLink(actionName.Titleize(), actionName, controllerName))
.AppendHtml("</li>");
}
return breadcrumb.AppendHtml("</ol>");
}
}
}
~/Extensions/StringExtensions.cs remains the same as below (scroll down to see the MVC5 version).
In razor view, we don't need Html.Raw, as Razor takes care of escaping when dealing with IHtmlContent:
....
....
<div class="container body-content">
<!-- #region Breadcrumb -->
#Html.BuildBreadcrumbNavigation()
<!-- #endregion -->
#RenderBody()
<hr />
...
...
ASP.NET 4, MVC 5 Solution
=== ORIGINAL / OLD ANSWER BELOW ===
(Expanding on Sean Haddy's answer above)
If you want to make it extension-driven (keeping Views clean), you can do something like:
In ~/Extesions/HtmlExtensions.cs:
(compatible with MVC5 / bootstrap)
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace YourProjectNamespace.Extensions
{
public static class HtmlExtensions
{
public static string BuildBreadcrumbNavigation(this HtmlHelper helper)
{
// optional condition: I didn't wanted it to show on home and account controller
if (helper.ViewContext.RouteData.Values["controller"].ToString() == "Home" ||
helper.ViewContext.RouteData.Values["controller"].ToString() == "Account")
{
return string.Empty;
}
StringBuilder breadcrumb = new StringBuilder("<ol class='breadcrumb'><li>").Append(helper.ActionLink("Home", "Index", "Home").ToHtmlString()).Append("</li>");
breadcrumb.Append("<li>");
breadcrumb.Append(helper.ActionLink(helper.ViewContext.RouteData.Values["controller"].ToString().Titleize(),
"Index",
helper.ViewContext.RouteData.Values["controller"].ToString()));
breadcrumb.Append("</li>");
if (helper.ViewContext.RouteData.Values["action"].ToString() != "Index")
{
breadcrumb.Append("<li>");
breadcrumb.Append(helper.ActionLink(helper.ViewContext.RouteData.Values["action"].ToString().Titleize(),
helper.ViewContext.RouteData.Values["action"].ToString(),
helper.ViewContext.RouteData.Values["controller"].ToString()));
breadcrumb.Append("</li>");
}
return breadcrumb.Append("</ol>").ToString();
}
}
}
In ~/Extensions/StringExtensions.cs:
using System.Globalization;
using System.Text.RegularExpressions;
namespace YourProjectNamespace.Extensions
{
public static class StringExtensions
{
public static string Titleize(this string text)
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text).ToSentenceCase();
}
public static string ToSentenceCase(this string str)
{
return Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLower(m.Value[1]));
}
}
}
Then use it like (in _Layout.cshtml for example):
....
....
<div class="container body-content">
<!-- #region Breadcrumb -->
#Html.Raw(Html.BuildBreadcrumbNavigation())
<!-- #endregion -->
#RenderBody()
<hr />
...
...
There is a tool to do this on codeplex: http://mvcsitemap.codeplex.com/ [project moved to github]
Edit:
There is a way to derive a SiteMapProvider from a database: http://www.asp.net/Learn/data-access/tutorial-62-cs.aspx
You might be able to modify the mvcsitemap tool to use that to get what you want.
I built this nuget package to solve this problem for myself:
https://www.nuget.org/packages/MvcBreadCrumbs/
You can contribute here if you have ideas for it:
https://github.com/thelarz/MvcBreadCrumbs
For those using ASP.NET Core 2.0 and looking for a more decoupled approach than vulcan's HtmlHelper, I recommend having a look at using a partial view with dependency injection.
Below is a simple implementation which can easily be molded to suit your needs.
The breadcrumb service (./Services/BreadcrumbService.cs):
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System;
using System.Collections.Generic;
namespace YourNamespace.YourProject
{
public class BreadcrumbService : IViewContextAware
{
IList<Breadcrumb> breadcrumbs;
public void Contextualize(ViewContext viewContext)
{
breadcrumbs = new List<Breadcrumb>();
string area = $"{viewContext.RouteData.Values["area"]}";
string controller = $"{viewContext.RouteData.Values["controller"]}";
string action = $"{viewContext.RouteData.Values["action"]}";
object id = viewContext.RouteData.Values["id"];
string title = $"{viewContext.ViewData["Title"]}";
breadcrumbs.Add(new Breadcrumb(area, controller, action, title, id));
if(!string.Equals(action, "index", StringComparison.OrdinalIgnoreCase))
{
breadcrumbs.Insert(0, new Breadcrumb(area, controller, "index", title));
}
}
public IList<Breadcrumb> GetBreadcrumbs()
{
return breadcrumbs;
}
}
public class Breadcrumb
{
public Breadcrumb(string area, string controller, string action, string title, object id) : this(area, controller, action, title)
{
Id = id;
}
public Breadcrumb(string area, string controller, string action, string title)
{
Area = area;
Controller = controller;
Action = action;
if (string.IsNullOrWhiteSpace(title))
{
Title = Regex.Replace(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(string.Equals(action, "Index", StringComparison.OrdinalIgnoreCase) ? controller : action), "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLower(m.Value[1]));
}
else
{
Title = title;
}
}
public string Area { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public object Id { get; set; }
public string Title { get; set; }
}
}
Register the service in startup.cs after AddMvc():
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddTransient<BreadcrumbService>();
Create a partial to render the breadcrumbs (~/Views/Shared/Breadcrumbs.cshtml):
#using YourNamespace.YourProject.Services
#inject BreadcrumbService BreadcrumbService
#foreach(var breadcrumb in BreadcrumbService.GetBreadcrumbs())
{
<a asp-area="#breadcrumb.Area" asp-controller="#breadcrumb.Controller" asp-action="#breadcrumb.Action" asp-route-id="#breadcrumb.Id">#breadcrumb.Title</a>
}
At this point, to render the breadcrumbs simply call Html.Partial("Breadcrumbs") or Html.PartialAsync("Breadcrumbs").
Maarten Balliauw's MvcSiteMapProvider worked pretty well for me.
I created a small mvc app to test his provider: MvcSiteMapProvider Test (404)
For whoever is interested, I did an improved version of a HtmlExtension that is also considering Areas and in addition uses Reflection to check if there is a Default controller inside an Area or a Index action inside a Controller:
public static class HtmlExtensions
{
public static MvcHtmlString BuildBreadcrumbNavigation(this HtmlHelper helper)
{
string area = (helper.ViewContext.RouteData.DataTokens["area"] ?? "").ToString();
string controller = helper.ViewContext.RouteData.Values["controller"].ToString();
string action = helper.ViewContext.RouteData.Values["action"].ToString();
// add link to homepage by default
StringBuilder breadcrumb = new StringBuilder(#"
<ol class='breadcrumb'>
<li>" + helper.ActionLink("Homepage", "Index", "Home", new { Area = "" }, new { #class="first" }) + #"</li>");
// add link to area if existing
if (area != "")
{
breadcrumb.Append("<li>");
if (ControllerExistsInArea("Default", area)) // by convention, default Area controller should be named Default
{
breadcrumb.Append(helper.ActionLink(area.AddSpaceOnCaseChange(), "Index", "Default", new { Area = area }, new { #class = "" }));
}
else
{
breadcrumb.Append(area.AddSpaceOnCaseChange());
}
breadcrumb.Append("</li>");
}
// add link to controller Index if different action
if ((controller != "Home" && controller != "Default") && action != "Index")
{
if (ActionExistsInController("Index", controller, area))
{
breadcrumb.Append("<li>");
breadcrumb.Append(helper.ActionLink(controller.AddSpaceOnCaseChange(), "Index", controller, new { Area = area }, new { #class = "" }));
breadcrumb.Append("</li>");
}
}
// add link to action
if ((controller != "Home" && controller != "Default") || action != "Index")
{
breadcrumb.Append("<li>");
//breadcrumb.Append(helper.ActionLink((action.ToLower() == "index") ? controller.AddSpaceOnCaseChange() : action.AddSpaceOnCaseChange(), action, controller, new { Area = area }, new { #class = "" }));
breadcrumb.Append((action.ToLower() == "index") ? controller.AddSpaceOnCaseChange() : action.AddSpaceOnCaseChange());
breadcrumb.Append("</li>");
}
return MvcHtmlString.Create(breadcrumb.Append("</ol>").ToString());
}
public static Type GetControllerType(string controller, string area)
{
string currentAssembly = Assembly.GetExecutingAssembly().GetName().Name;
IEnumerable<Type> controllerTypes = Assembly.GetExecutingAssembly().GetTypes().Where(o => typeof(IController).IsAssignableFrom(o));
string typeFullName = String.Format("{0}.Controllers.{1}Controller", currentAssembly, controller);
if (area != "")
{
typeFullName = String.Format("{0}.Areas.{1}.Controllers.{2}Controller", currentAssembly, area, controller);
}
return controllerTypes.Where(o => o.FullName == typeFullName).FirstOrDefault();
}
public static bool ActionExistsInController(string action, string controller, string area)
{
Type controllerType = GetControllerType(controller, area);
return (controllerType != null && new ReflectedControllerDescriptor(controllerType).GetCanonicalActions().Any(x => x.ActionName == action));
}
public static bool ControllerExistsInArea(string controller, string area)
{
Type controllerType = GetControllerType(controller, area);
return (controllerType != null);
}
public static string AddSpaceOnCaseChange(this string text)
{
if (string.IsNullOrWhiteSpace(text))
return "";
StringBuilder newText = new StringBuilder(text.Length * 2);
newText.Append(text[0]);
for (int i = 1; i < text.Length; i++)
{
if (char.IsUpper(text[i]) && text[i - 1] != ' ')
newText.Append(' ');
newText.Append(text[i]);
}
return newText.ToString();
}
}
If can definitely can be improved (probably does not cover all the possible cases), but it did not failed me until now.
I'm new to the MVC framework and wondering how to pass the RSS data from the controller to a view. I know there is a need to convert to an IEnumerable list of some sort. I have seen some examples of creating an anonymous type but can not figure out how to convert an RSS feed to a generic list and pass it to the view.
I don't want it to be strongly typed either as there will be multiple calls to various RSS feeds.
Any suggestions.
I've been playing around with a way of doing WebParts in MVC which are basically UserControls wrapped in a webPart container. One of my test UserControls is an Rss Feed control. I use the RenderAction HtmlHelper extension in the Futures dll to display it so a controller action is called. I use the SyndicationFeed class to do most of the work
using (XmlReader reader = XmlReader.Create(feed))
{
SyndicationFeed rssData = SyndicationFeed.Load(reader);
return View(rssData);
}
Below is the controller and UserControl code:
The Controller code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Security;
using System.IO;
namespace MvcWidgets.Controllers
{
public class RssWidgetController : Controller
{
public ActionResult Index(string feed)
{
string errorString = "";
try
{
if (String.IsNullOrEmpty(feed))
{
throw new ArgumentNullException("feed");
}
**using (XmlReader reader = XmlReader.Create(feed))
{
SyndicationFeed rssData = SyndicationFeed.Load(reader);
return View(rssData);
}**
}
catch (ArgumentNullException)
{
errorString = "No url for Rss feed specified.";
}
catch (SecurityException)
{
errorString = "You do not have permission to access the specified Rss feed.";
}
catch (FileNotFoundException)
{
errorString = "The Rss feed was not found.";
}
catch (UriFormatException)
{
errorString = "The Rss feed specified was not a valid URI.";
}
catch (Exception)
{
errorString = "An error occured accessing the RSS feed.";
}
var errorResult = new ContentResult();
errorResult.Content = errorString;
return errorResult;
}
}
}
The UserControl
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Index.ascx.cs" Inherits="MvcWidgets.Views.RssWidget.Index" %>
<div class="RssFeedTitle"><%= Html.Encode(ViewData.Model.Title.Text) %> <%= Html.Encode(ViewData.Model.LastUpdatedTime.ToString("MMM dd, yyyy hh:mm:ss") )%></div>
<div class='RssContent'>
<% foreach (var item in ViewData.Model.Items)
{
string url = item.Links[0].Uri.OriginalString;
%>
<p><a href='<%= url %>'><b> <%= item.Title.Text%></b></a>
<% if (item.Summary != null)
{%>
<br/> <%= item.Summary.Text %>
<% }
} %> </p>
</div>
with the code behind modified to have a typed Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
namespace MvcWidgets.Views.RssWidget
{
public partial class Index : System.Web.Mvc.ViewUserControl<SyndicationFeed>
{
}
}
#Matthew - perfect solution - as an alternative to code behind which tends to break the MVC concept, you can use:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SyndicationFeed>" %>
<%# Import Namespace="System.ServiceModel.Syndication" %>
Using MVC you don't even need to create a view, you can directly return XML to the feed reader using the SyndicationFeed Class.
(Edit) .NET ServiceModel.Syndication - Changing Encoding on RSS Feed this is a better way. (snip from this link instead.)
http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx
public ActionResult RSS(string id)
{
return return File(MyModel.CreateFeed(id), "application/rss+xml; charset=utf-8");
}
In MyModel
CreateFeed(string id)
{
SyndicationFeed feed = new SyndicationFeed( ... as in the MS link above)
.... (as in the MS link)
//(from the SO Link)
var settings = new XmlWriterSettings
{
Encoding = Encoding.UTF8,
NewLineHandling = NewLineHandling.Entitize,
NewLineOnAttributes = true,
Indent = true
};
using (var stream = new MemoryStream())
using (var writer = XmlWriter.Create(stream, settings))
{
feed.SaveAsRss20(writer);
writer.Flush();
return stream.ToArray();
}
}
A rss is a xml file with special format. You may design a dataset with that generic format and read the rss(xml) with ReadXml method and the uri as the path to the file. Then you have got a dataset you can consume from another clases.