Archives for category: MVC

I attempted to create an RSS in an MVC 2.0 application using my typical C# Web forms approach and received the following error:  OutputStream is not available when a custom TextWriter is used.

The main reason for this is that MVC Views assume the request will be in standard HTML and so it switches the TextWriter with its own writer.

The easiest way to get this done, then, is to handle the logic at the Controller level, rather than the View level.

1) First you will need to include a couple of libraries:

using System.Xml;
using System.ServiceModel.Syndication;

2) Then you want to create a custom RssActionResult, which the Controller will return:

public class RssActionResult : ActionResult
{
public SyndicationFeed Feed { get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = “application/rss+xml”;

        Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
rssFormatter.WriteTo(writer);
}
}
}

3) Then you want to set the Controller response to your custom result:

SyndicationFeed feed = new SyndicationFeed(
“IDEAInbox”,
“IDEAInbox Description”,
new Uri(“
http://IDEAInbox”),
“IDEAInboxSyndicationV1.0”,
DateTime.Now);
List<Idea> _Ideas = _myIdeaInboxservice.GetIdeas(-1, -1, 1);
List<SyndicationItem> items = new List<SyndicationItem>();
foreach (Idea _Idea in _Ideas)
{
SyndicationItem item = new SyndicationItem(_Idea.IdeaSubject,
_Idea.IdeaDescription,
new Uri(_Idea.Link),
“IDEAInboxIdeaID” + _Idea.IdeaID.ToString(),
_Idea.StatusInformation.CreateDateTime);
items.Add(item);
}
feed.Items = items;
return new RssActionResult() { Feed = feed };

Advertisements

I struggled a bit attempting to generate dynamic javascript in an MVC Razor page view.  I kept getting the following error:  Too many characters in character literal.

I did a little research and found the answer.  You will need to include
@using System.Text; at the top of your page view.

To build my javascript, I included the following code block within my <script> tags:

@{
StringBuilder sb = new StringBuilder();
foreach (GetBillingCodes_Result billingCode in Model.BillingCodes)
{
sb.Append(“AddBillingCode(‘” + billingCode.billing_code + “’”);
sb.Append(“, ‘” + billingCode.travel + “’”);
sb.Append(“, ‘” + billingCode.billing_code_type + “’”);
sb.Append(“, ‘” + billingCode.billing_rate + “’”);
sb.Append(“, ‘” + billingCode.taxable + “’”);
sb.Append(“, ‘” + billingCode.discount + “’”);
sb.Append(“, ‘” + billingCode.C100_club + “’”);
sb.Append(“, ‘” + billingCode.active + “’”);
sb.Append(“);”);
}
@MvcHtmlString.Create(sb.ToString());
}

I was deploying my MVC3 application into a production that did not have the proper DLLs in the GAC.  So I used the handy little tool in Visual Studio that allows you to include the DLLs in the BIN folder.  Simply right-click on the project in Visual Studio and select Add Deployable Dependencies.

image

But when I deployed the project into production, it kept attempting to redirect me to Account/Login, which does not exist.  And I have not routed those values anywhere.

It turns out that the WebMatrix.Data.dll was copied into my BIN directory and into production.  This DLL is the culprit.  I removed all instances of this DLL from my project, and the issue went away.

image