Note to self: Use this when you eventually get around to styling this blog!
http://prismjs.com/
Wednesday, February 4, 2015
Tuesday, February 3, 2015
MVC Routing and Areas - Can't find your view?
MVC Routing is weird...
We're refactoring an app to use Areas to break up our app into more manageable pieces. I moved some controllers to the new "Internal" area and tried to access it. It couldn't find the controller by accessing ~/Internal/Controller, but my code would be hit if I did ~/Controller, even though the controller had been moved to the new Area.
Even when hit, it couldn't find the view by calling return View(model);
I added a new controller to the area controller's folder and used that namespace for my existing controller.
Now it works! However, my controller will still be hit if I call ~/Controller or ~/Internal/Controller.
MVC must look at the namespace to determine routing, which I think is unfortunate.
So, if you're having an issue with a controller you moved to an area, make sure you have the correct namespace!
We're refactoring an app to use Areas to break up our app into more manageable pieces. I moved some controllers to the new "Internal" area and tried to access it. It couldn't find the controller by accessing ~/Internal/Controller, but my code would be hit if I did ~/Controller, even though the controller had been moved to the new Area.
Even when hit, it couldn't find the view by calling return View(model);
I added a new controller to the area controller's folder and used that namespace for my existing controller.
Now it works! However, my controller will still be hit if I call ~/Controller or ~/Internal/Controller.
MVC must look at the namespace to determine routing, which I think is unfortunate.
So, if you're having an issue with a controller you moved to an area, make sure you have the correct namespace!
Tuesday, October 14, 2014
Array and Model to MVC Controller Method via jQuery Post()
Posting this as a reminder on how to make the jQuery post() method work with MVC when passing an array.
Monday, September 15, 2014
Google OAuth 2 - The Hard Way
So... Google has deprecated their old single signon code. Unfortunately I'd added that code to 3 different applications. So, we need to move forward!
I tried using the Microsoft WebPages OAuth Library, but the Google provider wouldn't work properly. Tried following the directions here
http://www.beabigrockstar.com/blog/google-signin-for-asp-net-mvc/
... but I was too stupid to "get it" and make it work. Getting weird errors on Google's end. I was already on 2 urgent projects, and now I have to make our Google login work again. It's just web requests, right? I can do some requests, no biggie. Found this site...
https://developers.google.com/oauthplayground/
which is everything the Google OAuth documentation isn't. Gave me everything I needed! Used a lot of code from Jerrie Peelser's blog (http://www.beabigrockstar.com/) which was a huge help. I'm thinking maybe the permissions from the Google dev site just hadn't propagated. Regardless, I created a helper class that just "does it".
You can modify it to suit your needs.
The "GetLoginRedirectUrl" code can be modified to specify which API's you want to access. The "GetAuthenticatedUserEmail" method is just a stupid simple method I use to get the email address. You can request any API that you put in the "GetAuthenticatedUserEmail" method no problem.
I hope you find this useful!
I tried using the Microsoft WebPages OAuth Library, but the Google provider wouldn't work properly. Tried following the directions here
http://www.beabigrockstar.com/blog/google-signin-for-asp-net-mvc/
... but I was too stupid to "get it" and make it work. Getting weird errors on Google's end. I was already on 2 urgent projects, and now I have to make our Google login work again. It's just web requests, right? I can do some requests, no biggie. Found this site...
https://developers.google.com/oauthplayground/
which is everything the Google OAuth documentation isn't. Gave me everything I needed! Used a lot of code from Jerrie Peelser's blog (http://www.beabigrockstar.com/) which was a huge help. I'm thinking maybe the permissions from the Google dev site just hadn't propagated. Regardless, I created a helper class that just "does it".
You can modify it to suit your needs.
The "GetLoginRedirectUrl" code can be modified to specify which API's you want to access. The "GetAuthenticatedUserEmail" method is just a stupid simple method I use to get the email address. You can request any API that you put in the "GetAuthenticatedUserEmail" method no problem.
I hope you find this useful!
Tuesday, July 22, 2014
Cheesie simple modal dialog
From time to time I have to work on projects where I have old versions of libraries, like jQuery, and I can't upgrade them. Yet, I need some features of libraries I like to use, like Bootstrap. This time, I needed a simple wait dialog that would show up, then go away when I needed it to. I'm working with jQuery 1.4.4 and jQueryUI is just too much for what I'm doing.
So, I found a great article on the web for a simple CSS positioned modal dialog.
http://blog.raventools.com/create-a-modal-dialog-using-css-and-javascript/
And my version is below. All I really changed is some styling, added a variable for content, which I'm setting statically, and have the styles inline instead of in a separate style sheet. This is a quick and dirty fix for a dying project. I've done a better version with bootstrap/angular but that's a different post :)
So, I found a great article on the web for a simple CSS positioned modal dialog.
http://blog.raventools.com/create-a-modal-dialog-using-css-and-javascript/
And my version is below. All I really changed is some styling, added a variable for content, which I'm setting statically, and have the styles inline instead of in a separate style sheet. This is a quick and dirty fix for a dying project. I've done a better version with bootstrap/angular but that's a different post :)
Monday, June 30, 2014
MVC - Saving Files AND Json Model
When posting files in MVC using AJAX, Microsoft gives us a great tool, the Request.Files collection. However, when passing files via ajax, MVC ignores your json model that you pass as well. You can see the way around this in the code snippets below.
And in our javascript, using jQuery's ajax() method...
I add the json model to the FormData collection. Once that's done, I can pull it from the Request.Form[] Name Value collection when it posts to the server.
And in our javascript, using jQuery's ajax() method...
I add the json model to the FormData collection. Once that's done, I can pull it from the Request.Form[] Name Value collection when it posts to the server.
WebApi and OData - Without Entity Framework
OData is pretty nice, all said. Makes displaying data in a grid way easier. Microsoft has done some pretty cool things with WebAPI, gotten rid of the last bits of weirdness that remain in MVC (imo). However, their odata implementation adds more typical MS weirdness...
While most oData parameters just "work" and apply directly to your IQueryable, unfortunately $inlinecount isn't one of them.
Fortunately there's a pretty easy workaround.
Add WebApi and oData using NuGet.
Do this to your method.
The key to this is returning the OData PageResult. It's pretty neat that MS' oData implementation will just work on your IQueryable, without modification. It's just that WebAPI won't return a PageResult when it sees the $inlinecount parameter, like it will when you use an oData controller. My issue with the oData controller is that it forces you into exposing your entire model for each object in your graph. I'm not trying to expose all of my data, I'm just trying to get an easy oData endpoint to feed my sites. Rarely do I need to expose my entire object graph to the user, and those queries are way too heavy. I also don't want a lot of JS baggage by filtering and selecting only the properties I need. This solution works much better for me.
While most oData parameters just "work" and apply directly to your IQueryable, unfortunately $inlinecount isn't one of them.
Fortunately there's a pretty easy workaround.
Add WebApi and oData using NuGet.
Do this to your method.
The key to this is returning the OData PageResult. It's pretty neat that MS' oData implementation will just work on your IQueryable, without modification. It's just that WebAPI won't return a PageResult when it sees the $inlinecount parameter, like it will when you use an oData controller. My issue with the oData controller is that it forces you into exposing your entire model for each object in your graph. I'm not trying to expose all of my data, I'm just trying to get an easy oData endpoint to feed my sites. Rarely do I need to expose my entire object graph to the user, and those queries are way too heavy. I also don't want a lot of JS baggage by filtering and selecting only the properties I need. This solution works much better for me.
Subscribe to:
Posts (Atom)