Validation of ViewState MAC Failed - c#

Sorry if this is duplicate but I've been going crazy for the past two hours over this.
After changing the Master Page in ASP.NET MVC 1.0 application, I keep getting this familiar error when I try a postback without filling in the mandatory form elements which are validated by the server:
"Validation of viewstate MAC failed.
If this application is hosted by a Web
Farm or cluster, ensure that
configuration specifies
the same validationKey and validation
algorithm. AutoGenerate cannot be used
in a cluster."
The new page refers to a lot of jQuery code with lightboxes, superfish etc. Could that be a problem while doing a postback?
If I revert back to the original master, the error disappears and I'm able to validate form fields. Both masters are located in the same path.
I know a lot of other guys have faced this issue but I was unable to find anything which could help me.
Thanks.
Edited and added
After a little debugging, I've realized that a directive in the master page:
<% Html.RenderAction("menu", "nav"); %>
is creating the problem. The directive asks the "menu" action of the controller "nav" to inject a partial view Menu.ascx. If I delete this line from the new master page, everything works OK. My app's left bar navigation relies on this directive to work properly. Is there any way I can get around this? Very mysterious.

Are you using Html.AntiForgeryToken() anywhere on this page? Sometimes when I am testing locally with multiple different sites and / or port changes this will happen to me. If it does I clear my browser cache and it works just fine.

I did experienced the same problem two days ago. :)
A simple restart of the box has worked in my case so I did'nt investigate further.

Related

MVC 5 C# Paths change after publishing asp.net project

So this is going to bed a dumb question because I understand there is a simple solution, I just cannot figure it out....
At the moment I am running my MVC 5 project in visual studio 2013... Normally when I want to debug it I will just click run and the website will appear in the browser of my choice...
I just recently published it to my companys web server and now I am having all kinds of issues...
The website is just 15 buttons on the Home/Index page and I want to be able to click on the buttons and still run them on my localhost side and when I publish them, still have them find the right path..
Here is an example because I am not very good at explaining things...
I run it in visual studio my url is:
http://localhost:54641/Home/Index
I run my published site on the webserver my url is:
http://webserver/racklabels/dakkota/Home/Index
I understand this part, and in either case both of them work but when it comes to clicking on a button, or images.. the pathing is all weird and I dont understand it.
In visual studio I click my first button and my url becomes:
http://localhost:54641/RecieveTruck/AddFor
On my webserver I click it and it turns to:
http://webserver/RecieveTruck/AddFor
but gives me the 404 error because it is not the correct route ( which should be..
http://webserver/racklabels/dakkota/RecieveTruck/AddFor
)
This is how I have my button setup in my View (Index.cshtml) and I am wondering if this is what is causing the probelm (I am thinking that I may have to do it a different way other than using onclick)
<input type="button" name="receive" value="Receive Truck" style="cursor: pointer;" class="bodyText"
onmouseover="this.style.color='orangered';" onmouseout="this.style.color='black';"
onclick="document.location.href='/RecieveTruck/AddForm';">
Basically I would like for this to work regardless of how many folders I am deep after I publish it and when I click run in visual studio (so I can debug/test and just republish to the webserver when I want to update)
I have tried many different ways of sending the path... like '~/RecieveTruck/AddForm' (which just adds the ~ to the address) and '../RecieveTruck/AddForm' and just 'RecieveTruck/AddForm' but none of them find the actual root of where the project lives and just adds this to it...
Can anybody help me?
You can use
#Url.Content("~/Home/Index")
but this completely misses out the benefit of mvc routing.
if you want to generate urls for controller actions, it's much better to use
#Url.Action("Index","Home")
...so now if you set up some snappy routing rule, the url in provided by .Action will adjust automagically (such as omitting url parts that have default values in your routing rules).
You need to add "~" to the front of the url document.location.href='~/RecieveTruck/AddForm'
or if that does not work use document.location.href='#Url.Content("~/RecieveTruck/AddForm")'

The name Repeater1 does not exist in the current content

Im using a masterpage on asp.net c# for the website. (And created an aspx page related to the master page, by hand, not automatically)
I put a repeater control to the page.
Then I couldn't reach to the control by name in codebehind.
The compiling error is : 'The name Repeater1 does not exist in the current content'
Can you please point out my mistake?
Sometimes the designers get out of sync with the aspx pages. When such a thing occurs, the code behind files become unable to access controls that you have added to your aspx pages and you'll get errors like the one you received.
You can get around this error by regenerating the designer file, as you said Project -> Convert to web application is one way to do it.
One last thing, not sure what version of VS you're using, or if this will help, but there was a hotfix released for VS2010 that is supposed to take care of some occurrences of this problem.
http://blogs.msdn.com/b/webdev/archive/2010/03/05/hotfix-for-issue-with-auto-generated-designer-files-not-adding-controls.aspx

ASP Page naming and Redirection

I have quite the odd scenario I have not run into before.
I simply have a hyper link, with the url hard coded to www.mywebsite.com/StoreLoc.aspx
My application has two pages that we are concerned about.
Store.aspx
StoreLoc.aspx
Even though the hyperlink is hardcoded to go to StoreLoc.aspx, SOMETIMES ( once out of tenish ) the hyperlink will direct to Store.aspx instead.
I am not the first person to work on this application, so the aspx files very well may have been renamed. Could this cause this issue?
Thank you all for your time.
You might have something in the OnSesssionStart function of the Global.asax file the does a Response.Redirect to Store.aspx.
You session might be getting killed after x minutes of inactivity. This would cause the session to get started again and then get redirected to the start page.
You may want to search for Respose.redirect or Server.Transfer in your visual studio solution.
Hope this helps.

Can using server-side scripting delimiters (<% %>) cause compilations during runtime?

I am occasionally getting "Application compilation is starting." event in my Event Log and I can't identify what's causing it. I think I may try this - http://blogs.msdn.com/b/tess/archive/2008/11/06/troubleshooting-appdomain-restarts-and-other-issues-with-etw-tracing.aspx - but before I do that I was curious if I can identify the problem without starting to mess with something unknown.
I have used <%= %> and <%# %> tags throughout the app so I am wondering if this is what's causing the problems. On couple spots I have embedded C# code (using ) so that may add to it?
Precompiling the app is also valid choice for me, I just don't want to end in position in which I need execute precompilation command on the server every time I upload some changes to the server. Currently on my dev machine I've followed advices from this link - http://mikehadlow.blogspot.com/2008/05/compiling-aspx-templates-using.html - and it does awesome job as it allows me to identify errors in C# code in .aspx pages during build in Visual Studio. However, I presume precompilation results are not stored in my website directory (and won't be published when I use Publish option).
Ideally, I want to stay in position I am with default Web Application model with addition of automatically running compilation as soon as I upload changed .aspx or .ascx over FTP (not waiting for user's http request). Am I asking too much, or is this possible to setup?
From my research it seems it can.
Because nobody responded I'll accept this as answer.

Adding custom headers

I need to create and add custom headers to an ASP.NET 2.0 application.
The case is simulation of an SSO-login in our dev/test environment.
When I try to add headers I run into the "Not supported on this platform."
error. BigJim has a nice post on the subject here:
http://bigjimindc.blogspot.com/2007/07/ms-kb928365-aspnet-requestheadersadd.html
The root of my problem lies in the fact that I need to simulate various
persons logging into my application. Not just adding static data in a
HttpModule. I need to take values from a couple of TextBoxes and transfer
information from these into custom headers and then re-direct the user. The
HttpModule stuff happens to early in the pipeline...
Does anyone now if there exsist a simple redirect/proxy solution that one
could use in a dev environment? Or have simple/beautiful way of doing it in code?
One method i have used before, though a long winded approach, is NUnitASP.
This is based on the NUnit framework but intended for ASP.NET UI Testing.
It basically starts a browser in memory, and is able to manipulate the content exactly like a user would.
Using this you could view your page, enter data into textboxes and submit pages.
Hopefully that can help you do the testing you require. I've used it to test load, and spider through sites of mine to gather data.
If you use IIS 7 you can set the Pipeline Mode to integrated
This Setting is found in the App-Pool Properties.
I could be wrong, but doesn't the Response.AddHeader() method still work? Although, I agree with Oscar that a formal testing solution like NUnitASP is a good idea. Although, NUnitASP is a little dated. I still use it for some of my projects just because it still does work; it just isn't as refined or as simple as WaTiN or similar projects.
The browser drops the header if you do a Response.AddHeader()...
The header must be added to the orginal Request...
why don't use ASP.NET forms authentication model?
you define your "private folders". if you attempt to acces to a private folder without login, you automatically are redirected to a your custom login page.
here's a couple of link:
http://support.microsoft.com/kb/301240
http://www.asp.net/learn/security/tutorial-02-cs.aspx

Categories