Showing posts with label ASP.NET MVC. Show all posts
Showing posts with label ASP.NET MVC. Show all posts

2016-06-02

CS1056: Unexpected character '$'

When you get the message " CS1056: Unexpected character '$' " in runtime asp.net, it means that the front end file (html/aspx) is using a way of formatting strings which is called "string interpolation".
But to use string interpolation in your web frontend files you must put the following into your web.config file:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs"
      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
      type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
  </compilers>
</system.codedom>

Source:
http://stackoverflow.com/questions/30832659/string-interpolation-in-a-razor-view
 
Disclaimer:
The above worked for me, but may not work for all .NET Framework versions and/or ASP.NET versions.

2016-03-02

Catching mails

When developing a solution, sometimes it is nice to be able to catch mails and store them somewhere in stead of potentially sending them to real customers.

In web.config or app.config, there is a setting that can accomplish that for you:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="c:\Temp\Mail"/>
    </smtp>
  </mailSettings>
</system.net>
 If you install an eml-viewer or maybe just a regular mail client, you can open and read the mails from the folder you specified.

Source:
http://stackoverflow.com/questions/567765/how-can-i-save-an-email-instead-of-sending-when-using-smtpclient

2009-05-14

Getting started with S#arp Architecture

I am trying to get started with S#arp Architecture, and I found some nice (short) videos at Dime Casts.NET:

Introdction to S#arp Architecture

Another look at Sharp Architecture- Validation, Design Decisions and Automapping

Taking a look at how to modify the T4 templates used by Sharp Architecture

I’ll be looking at them and creating my own test project. Should be good :)

There should also be a good Northwind example available with the downloads from Google Code.

2009-05-02

FileUpload for ASP.NET MVC 1.0

I worked my way through the free Nerd Dinner chapter from ASP.NET MVC 1.0, creating my own web from the example. My web is a Food Recipe application where one can search among 7000 recipes on words in the title or ingredients.

The web should also be able to have pictures of the food, so I needed to do some file uploading, and so I found Scott Hanselman’s article http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx. I copied some of his code, and put the parts I needed into this function (the definition of the ViewDataUploadFilesResult class is in Scott’s article):

private List<ViewDataUploadFilesResult> uploadFiles()
{
    var r = new List<ViewDataUploadFilesResult>();

    foreach (string file in Request.Files)
    {
        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
        if (hpf.ContentLength == 0)
            continue;
        string savedFileName = Path.Combine(
           string.Concat(AppDomain.CurrentDomain.BaseDirectory,"images\\upload"),
           Path.GetFileName(hpf.FileName));
        hpf.SaveAs(savedFileName);

        r.Add(new ViewDataUploadFilesResult()
        {
            Name = savedFileName,
            Length = hpf.ContentLength
        });
    }
    return r;
}

Now, from before I had an Edit-action for the posting of my Edit View in my Controller, and from this I called the function above, as hown in the following code:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
    try
    {
        var recipe = recipeRepository.GetRecipe(id);
        recipe.RecipeDescription = Request.Form["RecipeDescription"];

// code removed for brevity

        List<ViewDataUploadFilesResult> fileUploaded = uploadFiles();

        if (fileUploaded.Count > 0)
            recipe.RecipePictureUrl = Path.GetFileName(fileUploaded[0].Name);

        recipeRepository.Save();
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

I also had to make some changes to my View:

First, I had to add an “enctype” to the form element. This is done like this with the Html-helper class:

<% using (Html.BeginForm("Edit", "<EntityController>", null, FormMethod.Post, new { @enctype = "multipart/form-data" })) {%>

Second, to be able to use a FileOpen dialog, I had to add an attribute to the text box for entering the file name. In plain old ASP/Html, you would use:

<input type=”file”>

And that is also what we need to do here, except we need to use the Html-helper class like this:

<%= Html.TextBox("RecipePictureUrl", Model.RecipePictureUrl, new { @type = "file" }) %>

2009-04-14

ASP.NET MVC 1.0

Rob Conery, Scott Hanselman, Phil Haack and Scott Guthrie have come up with a book on the Model-View-Controller framework, and the first chapter describes building a simple web site using the framework.

The chapter is free and can be downloaded from this link: http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf

The example web site is on the net: http://www.nerddinner.com

David Hayden has blogged about the book here: http://davidhayden.com/blog/dave/archive/2009/03/11/AnotherASPNETMVCSampleApplicationEBookTutorialNerddinner.aspx