loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 



Advertise Here
C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Run a Controller action from inside another Controller as the current User

by naspinski 12/15/2014 7:15:00 PM

manually creating a controller and running its action does not run as the logged in user

If you want to run an action of another controller from inside a separate controller, it will work fine *if* you do not need any current HttpContext values such as User, etc. If you want to do that, you will need to force that context into the controller instance. To do this, I added a simple method to my base Controller class:
public T RunAsCurrentUser(
    T controller, RouteData routeData = null) 
        where T : ControllerBase
{
    var newContext = new ControllerContext(
        new HttpContextWrapper(
            System.Web.HttpContext.Current), 
            routeData ?? new RouteData(), controller);
    controller.ControllerContext = newContext;
    return controller;
}

then to use it:
var someController = RunAsCurrentUser(
    new Some.NameSpace.SomeController());
var result = someController.SomeAction();

Kendo Grid "TypeError: u is undefined"

by naspinski 7/7/2014 8:48:00 PM

possible reason you may find this nondescript error

TypeError: u is undefined

I was getting this strange error while using KendoGrid which I had used many times before and I couldn't figure out why. I was simply calling:
$('#grid').kendoGrid();

On a simple table, nothing special. Turns out it was simply because I had used some <th> in the <tbody>, but it can only be located in the <thead>.

MVC Bundling not working on Azure deploy

by naspinski 1/29/2014 2:50:00 PM

working locally, but not on deploy

When using your bundler, everything may work fine, but quirks often pop up on Azure deployment, these are some of them I came across:

Styles/Javascript not rendering at all

This my be because your names resemble your directory structure, which apparently it does not like. For example, if this is your Bundle:
bundles.Add(new StyleBundle("~/Styles/Metro").Include(
            "~/Styles/Metro/metro-bootstrap.css",
            "~/Styles/Metro/metro-ui-icons.css"));

And you use the render:
@Styles.Render("~/Styles/Metro")

It will not render on Azure! Simply change it so your render string does not resemble your actual directory structure, and it will work:
bundles.Add(new StyleBundle("~/Styles/MetroCss").Include(
            "~/Styles/Metro/metro-bootstrap.css",
            "~/Styles/Metro/metro-ui-icons.css"));

And you use the render:
@Styles.Render("~/Styles/MetroCss")

Notice that the directory is ~/Styles/Metro but the render string is ~/Styles/MetroCss.

If that is not it, be sure you are using
@Styles.Render("~/Styles...")

for css, and:
@Scripts.Render("~/Scripts...")

for Javascript.

Images or Fonts not rendering

This is because you assume things will behave the same when they are up in Azure, which is not always the case. If you have this stuff outside of the ~/Content/ folder, your program will not, by default, know how to handle it. You will get a Controller error if you try to navigate to it manually.

The answer? Simply move everything somewhere within the ~/Content/ folder which MVC makes public by default. Don't forget to update your css and JavaScript accordingly!

Otherwise you can make a controller to handle these requests, but that involves more work.

KendoUI : TypeError: n.slice is not a function

by naspinski 12/17/2013 7:53:00 PM

common error in Kendo UI that has a simple solution

The error:
TypeError: n.slice is not a function

Is not very clear, but it is pretty simple. You are generally using some sort of AutoComplete, DataGrid, etc. This simply means that you are not returning a collection (JSON), but a layer (or more) up from that collection you want to include. For example, you would be returning this JSON:
{
    "TrackingModels":[
        {"Id":17,"Name":"Bill"},
        {"Id":17,"Name":"Bob"}
    ]
}

When what Kendo is expecting is just this:
{[
    {"Id":17,"Name":"Bill"},
    {"Id":17,"Name":"Bob"}
]}