Tech blog

Solving problems

About
Contact

Archive for the ‘ASP.NET MVC’ Category

PDF files not Publish in ASP.NET MVC when deploying

leave a comment

We faced a problem when we added some PDF files into our media library of ASP.NET MVC website and noticed that these files are not Published automatically.
So you need in Visual Studio to click on each PDF file and under Properties (F4) just choose

  • Build Action=Content
  • Copy to Output Directory=Copy Always
VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Written by Avivo

June 1st, 2010 at 1:28 pm

Combine Javascript and CSS in one request, minimization and optimization in ASP.NET MVC

leave a comment

There is an Combres project that can help you achieve this goal: http://combres.codeplex.com/

Here is a tutorial how to use it:

http://www.codeproject.com/KB/aspnet/combres2.aspx

1. Put this in web.config:
<configuration>
<configSections>
<section name="combres" type="Combres.ConfigSectionSetting, Combres, Version=2.0.0.0, Culture=neutral, PublicKeyToken=49212d24adfbe4b4"/>
...
</configSections>
<combres definitionUrl="~/App_Data/combine.xml"/>
...
</configuration>

2. Put this in Global.asax
At the top put this:

using Combres;



And then in the Application_Start put this:


protected void Application_Start()
{
//Apply combres compression
RouteTable.Routes.AddCombresRoute("Combres Route");
}

3. Add a reference in your project to merged Combres.dll library.

4. Create an xml file (named it as it is in web.config and put it in specified location):

<?xml version="1.0" encoding="utf-8" ?>
<combres xmlns='urn:combres'>
<resourceSets url="~/combres.axd" defaultDuration="30" defaultVersion="11">
<resourceSet name="siteCss" type="css">
<resource path="~/Styles/jquery.paginate.css" />
<resource path="~/Styles/general.css" />
<resource path="~/Styles/redmond/jquery-ui-1.8rc3.custom.css" />
<resource path="~/Styles/jquery.fancybox-1.3.0.css" />
<resource path="~/Styles/jquery.ratings.css" />
</resourceSet>
<resourceSet name="siteJs" type="js">
<resource path="~/Scripts/jquery-1.4.2.min.js" />
<resource path="~/Scripts/jquery-ui-1.8rc3.custom.min.js" />
<resource path="~/Scripts/jquery.ratings.js" />
<resource path="~/Scripts/jquery.paginate.js" />
<resource path="~/Scripts/jquery.apag.js" />
<resource path="~/Scripts/fancybox/jquery.easing-1.3.pack.js" />
<resource path="~/Scripts/fancybox/jquery.mousewheel-3.0.2.pack.js" />
<resource path="~/Scripts/fancybox/jquery.fancybox-1.3.0.pack.js" />
<resource path="~/Scripts/swfobject.js" />
</resourceSet>
</resourceSets>
</combres>

5. In the HEAD of yout HTML or master page call this:

<head runat="server">
<title>Some title...</title>
<%= Combres.WebExtensions.CombresLink("siteCss")%>
<%= Combres.WebExtensions.CombresLink("siteJs")%>
</head>

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

ASP .NET MVC: How to render View into a string

leave a comment

Sometimes a developer needs to render a View as string. Practical scenario would be to create an obvious View and send it via e-mail instead displaying it in browser. Here is a simple way to extract content from a rendered View:

Implement RenderViewToString method

public class HomeController : Controller
{
  protected string RenderViewToString(string viewName, object model)
  {
    string result = null;
    var view = ViewEngines.Engines.FindView(this.ControllerContext, viewName, null).View;
    if (view != null)
    {
      var sb = new StringBuilder();
      using (var writer = new StringWriter(sb))
      {
        var viewContext = new ViewContext(this.ControllerContext, view,
              new ViewDataDictionary(model), new TempDataDictionary(), writer);
        view.Render(viewContext, writer);
        writer.Flush();
      }
      result = sb.ToString();
    }
    return result;
  }
}

Call it in your action method

//Somewhere in HomeController...
public ActionResult Index()
{
    //Render a View named 'Email' to string variable named 'content'
    //Second parameter (model) can be null
    string content = RenderViewToString("Email", new EmailModel());

    //Do something with the content, e.g. send it to e-mail

    //This does nothing to do with rendered string
    return View();
}
VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Written by developer

March 31st, 2010 at 10:38 pm

Comparison: ASP .NET WebForms vs MVC

leave a comment

Comparison: ASP .NET WebForms vs MVC

WebForms MVC
  • Requires .NET 2.0
  • Event model – button click triggers an event in code-behind
  • ViewState – server-based forms for easier management
  • Existing third party controls
  • Requires .NET 3.5 and MVC library
  • Requires IIS 6.0 or later for URL rewritting – btw, max IIS version for WinXP is 5.1
  • Backward compatible – developer can include WebForms pages into MVC website and vice-versa
  • Dividing an application into the model, the view, and the controller – data, design and code are separated
  • For large teams of developers and designers (parallel work)
  • No ViewState means smaller output and cleaner html
  • Portable among other languages (like php) – similar file hierarchy, object names, different syntax
  • 30-50% faster than Web Forms, 100-800 requests per second
  • Native URL rewritting
  • Skinnable, suitable for websites that are viewed in computer browsers and mobile browsers
  • Unit testing
  • VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: +3 (from 3 votes)

    Written by Avivo

    January 30th, 2010 at 11:41 pm

    Firefox very (extremely) slow on Vista Cassini web server (local webserver in VS)

    leave a comment

    It turns out that the slowness is caused by an IPv6 issue with DNS and can easily be resolved by turning IPv6 support off in Firefox while doing localhost testing.

    To make the change do this:

    1. type about:config in the address bar
    2. locate the network.dns.disableIPv6 setting
    3. double-click on it to set it to true.

    This does the trick for the Firefox localhost issue on Vista and everything is running fast again.

    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

    Written by Avivo

    December 2nd, 2009 at 5:07 pm