Sitecore controller rendering causing StackOverflowException - asp.net

I am trying to do a simple controller rendering with Sitecore 8 and for some reason it's producing a StackOverflowException on the line within the main layout markup that contains the reference to the placeholder it is to be rendered in. This seems to crash the worker process, but you can see the stack overflow on debugging the process:
Here is my very basic controller:
And here is my controller rendering definition:
Reproduction notes:
This is occurring in a vanilla Sitecore 8 installation (rev. 150427 -installed via SIM).
The MVC project is also vanilla -created with empty ASP.NET project, then NuGetting in MVC 5.1.
Web.config & Global added to project from the Sitecore site root in wwwroot.
FYI - everything is absolutely fine doing a view rendering - it's just controller renderings that seem to be causing a problem

So the problem was actually pretty simple in the end.
Returning a ViewResult when the view is intended as a partial view (which all Sitecore renderings will be) then you must set the layout property in the markup to null:
#{
Layout = null;
}
Otherwise MVC will try to wrap the layout file around it, which of course contains your Sitecore placeholder, which causes an infinite loop and crashes the worker process with a StackOverflowException.
So in the context of Sitecore, either return a PartialViewResult or return a ViewResult with the layout set as null.

I guess there is something missing in placeholder setting, could you check in path sitecore/layout/placeholder setting?
There should be a placeholder key which you are trying to use.
Hope this will help
Cheers!!

I think the issue can be with method View() being called without any parameters which can cause re-rendering the whole Sitecore page again.
Try to add parameter to View() like that:
return View("/Views/Courses/Index.cshtml");
Or whatever is the path of the view you want to return.
EDIT:
As #David Masters found, for some reason the issue is with calling View instead of PartialView method with the full path as a parameter. The correct code is:
return PartialView("/Views/Courses/Index.cshtml");

Related

ASP.Net MVC null reference exception with TextArea name

I have a TextArea html helper method I'm calling in a foreach loop. Basically, when I initially load the View it works fine, but when i reload the View and load postback data, the same TextArea throws a NullReferenceException and yet the variable I'm using in the TextArea as the name of the TextArea is not null. I've attached a picture below for demonstration:
Sorry if it's difficult to see, the blue arrow below is pointing to the variable used to name the TextArea. Again, it works on initial load, but it errors out on postback when the page is reloaded. I'm not sure what's going on.
Are you using custom validation in your application? If so, this is a known issue in MVC 1 (fixed for MVC 2). See http://forums.asp.net/p/1377232/2908610.aspx for more context and a workaround.
This link seemed to provide the solution. Turns out I was not setting the model value after adding errors to the model state, so it was searching for the model values to place back into the TextArea, they were null of course, hence the error. Very obscure error in my opinion. This StackOverflow post was also very enlightening.

ASP.Net Routing with WebForms

I am trying to cut over an existing WebForms app to use Routing, and want to do it in phases. However, I am running into some issues with a particular route:
//I want to catch existing calls to .aspx pages, but force them through
// a route, so I can eventually drop the .aspx extension
new Route("{page}.aspx", new MyCustomRoute());
This isn't working at all, as calls to [SomePage].aspx are never tripping this route... If I change the route to look like this:
//Same thing sans .aspx extension
new Route("{page}", new MyCustomRoute());
All calls to [SomePage] are getting picked up. Any ideas?
Ok, so now I feel stupid...
Turns out there is this little property on the RouteCollection class called RouteExistingFiles which is false by default. Apparently ASP.Net routing gives precedence to existing files before turning them over to routing, so any calls to existing pages would obviously not be handled by my routes. Setting this property to true resolves my issue, all though it may have unintended side effects to which I am as of yet unaware.

Location of HtmlHelper instantiation for ASP.NET MVC

I am trying to debug a problem where HtmlHelper is null in extension methods. Oddly the debugger claims it's fully populated, however I consistently get null exceptions. I'm trying to figure out where HtmlHelper should being instantiated in an attempt to see where my problem may be.
Where should HtmlHelper be Instantiated?
Update: In particular I am trying to implement the extension found here, http://chriscavanagh.wordpress.com/2009/06/11/mvc-authorizedactionlink/, within a masterpage. The error occurs on the MVC template's HomeController.Index(). There are some 'plugins'/virtualpathing that may be causing the problem (trying to avoid this can of worms), but, code the code is essentially the same as found here: http://www.codeplex.com/unifico. However, I don't want to trouble anyone with the details of all of that.
The HtmlHelper is instantiated internally by ASP.NET MVC - it's not something you should generally need to worry about. Exactly where it gets instantiated depends on where you are using it.
The main place it gets instantiated is ViewPage's InitHelpers() method.
In ViewUserControls it gets created on-demand in the getter of the Html property.
In ViewMasterPages it just uses the ViewPage's Html property.
We've encountered this too (oddly, only on 1 of 3 machines), where we have a master page and the content page uses an HtmlHelper.DropDownList. What worked for us was adding using System.Web.Mvc to the master page's codebehind even though it was already in the content page's codebehind.

Strange Problem with asp.net website

I've built an asp website and i have the following issues:-
i'm using a master page in it and have defined two contentplaceholders one in head one in content, and i've specified the page title in the top most directive at the #page directive but the page title doesn't show up. I have to manually add a tag for it.
Secondly when i create a content page from a master page it creates it and when i rename it, it doesn't rename it's class. It remains _Default, thus every page was having an inherit to _default.
Most importantly
I'm using a page to enter and view data to the database. I've used a boolean called isadmin which i set according to credentials at page load. and i'm added a panel where it's visible property is set to Visible = '<%#IsAdmin %>'. It works properly when i run it through the visual studio environment but when i publish it and run it doesn't work and the panel just comes and stays there. Why is it happening? Any idea?
Thanks
The Visible problem is fixed as i had to enable windows authentication on the server. Awaiting answers for the other two issues. Thanks
Try to add a <head runat=server> to the master page. Only then ASP.net can "see" the tag and modify it
It is not that bad that several aspx-pages have identical class names. ASP.NET 2.0 started to process every page as its own compiling unit or so. Pages cannot see each other. There is a special directive to make pages see each other and instanciate or manipulate them. So it should not do much harm
Maybe you did not test this correctly and are mistaken that it DOES work in Visual Studio??? But in any case I would suggest that you move your logic into OnInit, then it runs much earlier. I think the control tree is build before Page.OnLoad. What you do is data binding, that might run only if DataBind is called, I'm not sure
Or use the safe way: Make IsAdmin a property so that it initializes itself on first call and caches the result in a variable
Regarding question #2 - add your content pages via Project -> Add New Item, and name it appropriately there. That way the naming is consistent and correct throughout.
Regarding #3, what HTML is output when you run it from the server?
your first issue can be solved by filling out the title part of the #Page directive in your .aspx pages. The master page will display that text in the browsers title bar.
and prob #2 should be solved by adding the files using the file add option in visual studio.

RegisterClientScriptResource + AJAX update panel

I have a problem that is really making me feel dumb. I have a custom control that inherits textbox. This textbox (at least for this example) simply has a .js file that is embedded in the .dll.
Long story short, works great when not in an AJAX update panel. So i did some research and found that instead of using: Page.ClientScript.RegisterClientScriptResource, i should use ScriptManger.RegisterClientScriptResource - i have done this in the overrdive render method, but the javascript still does not fire.
Anyone know what im doing wrong?
example:
i have a folder in my project called Scripts - it contains myscript.js
My Assembly is called Jim.Bob.Controls
I add attributes to my controls ie: Attributes.Add("onclick", "Test2();");
In the Override Render:
ScriptManager.RegisterClientScriptResource(this.Page, typeof(CustomTextBox), "Jim.Bob.Controls.Scripts.myscript.js");
Yet i still get 'object expected' error.
I need my textbox to work with and w/o AJAX. I imported System.Web.Extensions into my project to access ScriptManger
Can someone please tell me what im doing wrong? Again, this whole thing worked fine w/o AJAX, i have put the necessary stuff into the AssemblyInfo.cs (WebResource:,,,,etc)
Thanks in advance :-)
--- UPDATE ---
I reverted the control, trying it in a non AJAX web and i am having the same problem. Not sure why i have a problem, i have another custom control in the same assembly that is working just fine - have them setup the exact same way, only difference is the one that is working inherits WebControl, the one that is not inherits TextBox
...
in the one that is working i emmit html like Go and do it
Where the one that is NOT working i have
Attributes.Add("onclick", "CustomFunction();");
Also, if i do Attributes.Add("onclick", "alert('hello');");
it works fine.
Sorry for such a long post.
Try to pass this instead of this.Page. The ScriptManager would output scripts only for controls which are being updated (children of UpdatePanel that is).
I've just noticed that you are doing this during Render. That's too late. Try PreRender instead.

Resources