ZNAG - Browsing aspnetmvc

  1. Windows Live Writer Workaround for AtomSite 1.3

    [UPDATE - Available for version 1.4 as well, see posthere]

    In continuous to an old series about migrating from Blogger to AtomSite, I came up with a workaround/solution that allows you to connect Windows Liver Writer to the AtomSite blog.

    I've been asked a couple of times to supply the solution, so I thought I'd publish it here for more easy access:

    WLWWorkaround.zip

    Read the included README file for specific instructions..

    If you want an easier access to the workaround than clicking in the address mentioned in the README, you can add the following to your service.config, I added it "AdminSettingsEntireSite"=>"settingsRight"

    <svc:include  name="LiteralWidget">
        &lt;div class="widget settings area"&gt;
            &lt;h3&gt;Windows Live Writer Workaround&lt;/h3&gt;
            &lt;a href="/Admin/WLWWorkaround"&gt;Activate/Disable&lt;/a&gt;
        &lt;/div&gt;
    </svc:include>

    Small disclaimer, this has been tested on AtomSite 1.3 only..

    Enjoy!

    Erik

    Tags: asp.net mvc, aspnetmvc, Blog, AtomSite


  2. Using Mvc user controls from another controller

    I needed to use a user control from a different controllers folder in a view.

    Should be easy I thought, there’s probably an overload for the Html.RenderPartial that with the controller name.

    Unfortunately, that’s not the case.

     

    If you look at the solution layout below:

    SolutionLayout

     

    I needed to user the “OtherControl.ascx” from the Home controllers views.

     

    Passing only the user control name to the Html.RenderPartial won’t work, and you’ll end up with an error like this:

    Error

    Which is rather logical, as that’s the default behavior of the web forms view engine.

     

    Overcoming the problem is rather strait forward :

     

    <% Html.RenderPartial("../Other/OtherControl"new string[] { "ListItem1""ListItemN" }); %>

     

    Not the most elegant solution, but it works..

     

    Demo | Source

     

    Cheers

     

    Erik

    Tags: asp.net mvc, aspnetmvc, C#


  3. Asp.Net MVC Exception Handling with jQuery

    I stumbled upon this excellent post by Sumit :

    http://2leggedspider.wordpress.com/2009/12/22/handling-exceptions-using-jquery-and-asp-net-mvc/

     

    I thought the idea was great, but the fact that you need to parse it as JSON bothered me.

     

    Since the information needed is only the status code, stack trace and error message, it seemed more appropriate for me to use the existing http response parts that are designed to pass those values.

     

    For the impatient:

    Demo | Source

     

    [Update] Altered the code with @Neal, and @Colin's feedback..

     

    I created a filter that inherits the default [HandleError] Attribute.

     

    public class HandleErrorWithAjaxFilter : HandleErrorAttribute
    {
        public bool ShowStackTraceIfNotDebug { getset}
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                var content = ShowStackTraceIfNotDebug ||
                                filterContext.HttpContext.IsDebuggingEnabled ?
                                    filterContext.Exception.StackTrace :
                                    string.Empty;
                filterContext.Result = new ContentResult
                {
                    ContentType = "text/plain",//Thanks Colin
                    Content = content
                };
                filterContext.HttpContext.Response.Status =
                    "500 " + filterContext.Exception.Message
                    .Replace("\r"" ")
                    .Replace("\n"" ");
                filterContext.ExceptionHandled = true;
                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            }
            else
            {
                base.OnException(filterContext);
            }
        }
    }


    Note:

        The typical usage would be to decorate the controller with the[HandleErrorWithAjaxFilter]attribute.

        However, for the sake of the example, the actions are decorated separately to show the different override usages. 

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [ErrorsForAjax.Models.HandleErrorWithAjaxFilter]
        public ActionResult ThrowError()
        {
            throw new Exception("This is the error message");
        }

        [ErrorsForAjax.Models.HandleErrorWithAjaxFilter(ShowStackTraceIfNotDebug = true)]
        public ActionResult ThrowErrorWithStackTrace()
        {
            throw new Exception("This is another error message");
        }
    }

     

    The filter detects if the request came from Ajax, and if so returns a more slim response, allowing me to capture it in the jQuery ajax method's error handler:

     

    $(function() {
        $("button").click(function() {
            $.ajax({ error: function(xhr, status, error) {
                    xhr.statusText; //ErrorMessage
                    xhr.responseText; //StackTrace (if debug or overridden)
                    xhr.status; //Numeric status code
                },
                    url: '/SomeUrlThatMightThrowAnError'
                });
            });
            return false;
        });
    });

     

     

    Demo | Source

     

    Thanks again to Sumit for the great idea, and to Neal and Colin for the feedback.

     

    Erik

    Tags: aspnetmvc, asp.net mvc, jQuery


  4. ASP.NET MVC 2 Preview 1

    Phil Haack totally surprised with the announcement of the first preview release of ASP.NET MVC 2.

    I’ve been using ASP.NET MVC (1) very since the third preview was released and I’ve really enjoyed the the experience.

    IMHO, WebForms was never really fitted to the web platform, having a to complicated life cycle, and abusing the POST method in a way it was really not meant to do.

    I do think WebForms was a great way to get WinForm developers up and doing web development quickly,
    on the other hand, it grew a generation of Drag and Drop developers that had no idea what a POST really is (I know, I used to be one :D).

    One of the main problem in WebForms is that it’s simply to much magic going on. The server controls writes plenty of content to the page, and typically, the developer using the control has no idea, and/or no control of what is rendered to the page.

    For me the ASP.NET MVC brought a new fresher intention from Microsoft that I really liked.
    First, they involved the public a lot with several preview releases,
    second they embraced both concepts from other popular frameworks such as Ruby, and even bundled jQuery*!.

    This was somewhat of a mindblower for me, since it kind of ruined the typical Micro$oft big corporate image I had.

    Add to that the fact the the ASP.NET MVC framework was implemented in such an amazing way, that besides being enjoyable to develop in, felt much more correct and well suited for the web.

    The framework allowed me to program the server side code in C#, which is the language I’m most comfortable with, and supported a clean separation of markup, client side code and server side code.
    Furthermore, it was finally unit-testable!

    The ASP.NET MVC 2 looks really interesting, here’s some recommended reads:

    The fact that it’s possible to use without causing any problems with the existing MVC framework is fantastic..

    Kudos to the ASP.NET MVC team for their great efforts!

    Cheers,

    Erik

    *This was one of the reasons besides I got into jQuery more, although the credit goes to Tim Perrett for introducing jQuery to me amongst so much more ubercool stuff :D)

    Tags: aspnetmvc, C#, asp.net mvc, aspnetmvc2, asp.net mvc 2