Facebook canvas app ASP.NET MVC HttpException: Invalid Model - asp.net-mvc

I am working on a Facebook tab application. I am using asp.net MVC 2 with the "official" Microsoft Facebook SDK.
Most of my views are working just fine.
However, I have one that is causing a huge mess. I am simply returning a List<Tags> to the view and as my ViewModel. This problematic view is simply displaying the list in a foreach loop. Whenever navigate to this view I get a "System.Web.HttpException: Invalid Model" exception.
The most puzzling part of all this is that the same code is running without fail on our regular(non-Facebook) app that works just fine with the same code.
Any insight into why Facebook and asp.net MVC 2 would be causing this behavior would be much appreciated.

So, I finally figured out what was causing this dang problem.
It turned out to not be related to the View Model, the View or even the Action.
We are using another library. It is the Rest For Asp.NET MVC SDK. This library lets you decorate your action with [WebApiEnabled]. This allows those actions to return JSON or XML if the request has a ?format=json or ?format=xml.
If all of the actions in the controller should be Rest enabled then you can decorate the class instead of each method. Well this is what we did. And it was causing issues with the way that facebook grabs data.
So we moved the decoration from the class to the actions and the error went away.

Related

ASP.Net MVC verses WebAPI loading of initial Angular model

I have finished a few MVC partial views which load their data using calls to a webapi Get method, to preload the data used by the angular controller.
This method works but it feels more logical to do this via the initial asp.net-MVC Partial view load via #Model for example. Rather than waiting for the page to load and angular to call the get method of my webservice i could have prepopulated the Model, but im not sure how this would pass the data to Angular using this method.
I have had the same issue (if one call this an issue) and ended up doing binding the model to the partial view at the server side. The main rational for the decision was that the model was already available at the time at the server side and I was not building a Single Page Application.
Had I been developing a SPA, I would store the partials as templates at the client side, then grab the model via WebAPI and do the binding
If you use AngularJS,then it's not need ASP.NET MVC. Just use web api for get data.I written a demo site for AngularJS+ASP.NET WEB API,hope to help you,this is the source code.
Does your web page have a lot of heavy client-side interaction or are you simply using Angular to initialize the data for your page on load?
If there's a lot of client-side interaction, you will probably want to keep using Angular. If not, you might want to go back to using MVC since your use case doesn't really require Angular.

How to access the view page from a controller?

I've been tasked w/porting an older ASPX app to .NET MVC, and then doing some work on it.
This has been fraught with peril; not the least of my troubles is that the APSX events won't propagate in this framework (using ViewPage for the aspx.cs file - MVC 4 demands it)
I.E. - an asp:Button with a defined click event won't fire that click event anymore.
What prompted this particular question is this - I've got an asp:table that I want to reload when certain actions happen in the controller. In the code behind file, I've got a method that handles that, and it gets called from Page_Load, just fine.
But in the controller, I need to find a way to get a handle on that page itself and invoke that method. I can get a new instance of the page, but that instance won't have the table already instantiated.
How, in the controller, can I get a hold of the ViewPage created for that method?
You can't think about MVC that way. There are no click events like you think of them when you think ASP.NET.
read these
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
http://www.w3schools.com/aspnet/mvc_intro.asp
you might find some joy with them
these will help too
Migrating from ASP.NET WebForms to MVC
and Migrating legacy ASP.NET to MVC 2 (RC): HttpApplication events not firing, User principal is null

What's the best way to develop a debugging window for an ajax ASP.Net MVC application

While developing my ASP.NET MVC, I have started to see the need for a debugging console window to assist in figuring out what is going right and wrong in my code. I read the last few chapters of the Pro Asp.net MVC book, and the author details how to use http modules to show page load/creation times and linq to sql query logs, both of which I definitely want to be able to see. However, since I am loading a lot of small sections of my page individually with ajax I don't want the debug information right there in the middle of my screen.
So the idea I came up with was to have a separate browser window (open-able by a link or some javascript) with a console log, that can contain logged entries both from javascript and from the asp.net mvc run. The former should be relatively easy, but I'm having trouble coming up with a way to log the asp.net information in ajax requests.
The direction I have been thinking of going is to create an httpmodule (like the Pro MVC book does), and have that module contain some script tags that append the javascript's log to console calls with the messages. The issue I see with this is finding a way to get the log messages from the controller's action methods to the httpmodule's methods. The only way I see to do this is with a singleton, but I'm not sure if singletons are bad practice for a stateless web application.
Furthermore, it seems like if I return json with my ajax calls (instead of pure html) then that won't work at all unless there is a way to add data to an existing json structure inside the httpmodule.
How does everyone else handle this type of debugging in heavily ajax applications?
For reference, the javascript library I am using is jquery.
Why not Firebug or Fiddler? (or both? Together they do 99% of what you need.)

ASP.NET MVC CookieTempDataProvider: any experience?

UPDATE: looks like I've misunderstood what TempData is for and what it isn't. It definitively shouldn't be used to "keep certain session-wide data" as I asked initially (see ASP.NET MVC TempData Is Really RedirectData why). I've modified the question accordingly.
Has anyone used CookieTempDataProvider for TempData storage? Are there any caveats to watch out for (apart from keeping the session storage small)? Any issues with using it on Web farms?
I use the CookieTempDataProvider for our production web site and it seems to be working really well. We have a 2 server web farm. The site has been live for around 6 months, and we have experienced no issues, although the site does not get a lot of traffic.
I use the CookieTempDataProvider to store status messages that are to be displayed when a view loads. For example:
User edits a form and hits the save button. This is a post.
In the POST action method, I save the data, then push a confirmation message into TempData. Then I issue a RedirectToAction, to a GET action.
In the GET action method, I retrieve the message from the TempData and put it in the ViewData. Then I do my other data stuff and return the view.
On the view I check if the model has a message, and if so, display it.
Things to note:
I am using ASP.NET MVC 1.0.
I am using MVC Futures 1.0.
The CookieTempDataProvider did not work for me as is; I had to modify the code to get it working: see this post.

What's the best way to cache a user control or its associated data in asp.net mvc

I am in the middle of implementing an application using ASP.NET MVC and would love to cache the data passed to user controls or the output rendering on some user controls that I render using the Html.RenderPartial, that way I don't have to query the DB with every request I do to the controller for a new view.
That appears to be one of those 1 million dollar questions!
It seems that a lot of people are having that problem, but the solution is not trivial.
Check out an issue reported recently on the ASP.NET MVC Codeplex site...
I would maybe suggest using the sub controllers from the MVC Contrib and then caching the controller's method using the OutputCache stuff.

Resources