Validation of viewstate with GetCompiledPageInstance - c#

on Default.aspx I wrote this code it succesfully shows me Webform1.aspx but it has also one button when I click on that button it give me error
protected void Page_Load(object sender, EventArgs e)
{
var pageView = PageParser.GetCompiledPageInstance("~/WebForm1.aspx", Server.MapPath("~/WebForm1.aspx"), HttpContext.Current);
(pageView).ProcessRequest(HttpContext.Current);
}
Error on button click
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

I think you should try these:
define the following parameter in your both page directives after # Page in your .aspx files.
EnableViewState="false
Then add this in your webcofig in the system.web tag:
and modify your pages tag if exists or add as follows:
I do think it will serve your purpose.
PS: I am using all above in my own project in which I am creating instances of many webusercontrols in side my application and it works fine!

Related

aspx pages not working after adding application in IIS

i have webapplication. In my master page i have few link buttons which are as below
<asp:LinkButton ID="link1" runat="server" OnClick="linkAge_Click">Age</asp:LinkButton>
<asp:LinkButton ID="link2" runat="server" OnClick="linkName_Click">Name</asp:LinkButton>
In my Visual studio i have seperate folder called Age and Name and under those there are default.aspx.
on click event of link button i have this code
protected void linkAge_Click(object sender, EventArgs e)
{
Response.Redirect("/Age/");
}
protected void linkName_Click(object sender, EventArgs e)
{
Response.Redirect("/Name/");
}
In IIS i added Application called "Test" and then added all code inside it
When i browse i get to master page as http://localhost:80/Test
When i click on link "Age" the url Changes to http://localhost:80/Age
I expected it to be http://localhost:80/Test/Age
What is wrong that i am doing? Can i achieve this without using any code changes.
Try to use http://localhost/Test/ (with slash in the end) link in to Your browser.
You need to add a tilde (~) to get URLs relative to application root:
Response.Redirect("~/Age/");
BTW: Why do you need to have the redirect behind a post-back? You could use <asp:HyperLink> instead
Like we do this in mvc to get path from root - Url.Content("~\Action\")
In web form -
protected void linkAge_Click(object sender, EventArgs e)
{
Response.Redirect(Server.MapPath("~/Age/"));
}
You got to think like this i think.
Since you're using only the /Age/ path, with nothing to indicate that you're using a relative path. For code managed by IIS, you'd use ~/Age/Whatever to indicate that you intend to go to Age relative to Test.

Adding search parameter to url to enable direct search from address bar

I have a few old sites that I want to add routing parameters. They were coded without using mvc so there are no global.asax with the handy MVC settings.
Currently I have a page with the url abc.com/xyz that has a search function. I can input a query which would send me to another page but it has the same url. I want to make it so that if i put some variation of the url abc.com/xyz?search='what_You_Query', it gives me the searched page. Right now that url sends me to the page where I input my query.
The website is coded in C# and html and saved in aspx files. The webpages also make use of jscripts
I'd appreciate any help I can get
Edit: Seems like there was some confusion, there is a search box that allows the user to query on the webpage. What I want is to allow users to directly link to a searched page.
You'd need to capture that on the page load - check the query string (https://msdn.microsoft.com/en-us/library/ms524784%28v=vs.90%29.aspx) and if search is in it, redirect to the search page.
MODIFIED TO INCLUDE MORE DETAILS
I'm assuming your working with web forms (Microsoft alternative to MVC). You would need to add a server-side (http://www.seguetech.com/blog/2013/05/01/client-side-server-side-code-difference) Page_Load event (https://msdn.microsoft.com/en-us/library/6w2tb12s.aspx). There the code would look something like this:
protected void Page_Load(object sender, EventArgs e)
{
if(Request.QueryString["search"] != null)
Response.Redirect("/search?" + UrlEncode(Request.QueryString["search"]), true);
}
Please note I have not tested the code and am going by memory a bit here - but that should do the trick.

Uncaught ReferenceError: WebForm_DoPostBackWithOptions is not defined

I had a .Net 1.1 Webapplication which I have converted to the .Net 3.5 and published on the Windows Server 2012. There is a grid in a page where I need to delete a records with the cross button in the grid. But when I try to delete it shows the following error in the Chrome Console
GET
http://10.72.10.225/Models/SEIIAG/WebResource.axd?d=3IplHgLpPO4L8SEYBdyuKR9…fWWwa68LbCK6T7EgwTmR_WdnTJJbBQZJoJJGdKMX0cZCVl9eahgU1&t=635161970660000000
500 (Internal Server Error)
Uncaught ReferenceError: WebForm_DoPostBackWithOptions is not defined
Workaround 1:
In case the page where the problem is observed is based on .master page Sitefinity template.
Add the GetPostBackEventReference manually to the .master page in the OnPreRender event handler. In order to do that you will need to add a code-behind to the master page.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
var control = this.Controls[0];
this.Page.ClientScript.GetPostBackEventReference(control, string.Empty);
}
Workaround 2:
In case the page or page template uses asp:LinkButton controls change them to asp:Button controls.
Taken from Here
Got the solution
Go to IIS
Select your Virtual Directory
Select Handler Mappings
Double Click on that
Select *.axd and click Edit button
Change the value of executable section to
C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
Click Ok

ASP.Net Web application dll loading issue

I have a custom dll loaded into my web application, I updated it and reloaded the reference in my main web app. Basically the way it works is a user uploads a file that gets checked by a function in the dll. The user can then click a button on the results section that redirects to the main section of the app so they have a choice of rerunning the checker; I say section because the web app uses a single page with two divs, one for the form and one for the results, the code behind then swaps the visibility of the form div from the results div, here's the basic layout to clarify.
<body>
<div id="divForm" style="visibility: visible;">
</div>
<div id="divResults">
</div>
</body>
The redirect call is in the code behind as follows:
protected void btnReturn_Click(object sender, EventArgs e)
{
Response.Redirect("http://localhost/file_checker/");
}
And the main piece of the file checker:
protected void Page_Load(object sender, EventArgs e)
{
divForm.Visible = true;
divResults.Visible = false;
if (Page.IsPostBack)
{
//Do file checking
//Show results
divResults.Visible = true;
divForm.Visible = false;
}
}
The web app works fine on the first pass, however, I notice strange behavior after every subsequent pass. For example, I notice that the results page shows stale content from an older version of dll, i.e. It displays a string that was generated by an older version of the dll, I had removed the string and yet it still gets generated. I'm not sure if I'm redirecting correctly or if there's some other fundamental misunderstanding I have with how redirects work. Any help or insight is appreciated.
UPDATE:
Ok, instead of redirecting, I just cleared a gridview in my results section and swapped the styles of the divs to show the main section and hide the results section as follows:
protected void btnReturn_Click(object sender, EventArgs e)
{
//Clear the Gridview and show the upload Form
GridView1.DataSource = null; //<--Is popoulated by a DataTable
divForm.Visible = true;
divResults.Visible = false;
//Response.Redirect("http://localhost/file_checker/");
}
That seems to fix the problem, a lingering question I have is, I'm using a Datatable to populate the Gridview in the results page. Does the Datatable automatically dispose itself after each page load or button click event? Or will it persist? I just want to avoid any memory leak issues, since each page load instantiates a new DataTable object.
A Response.Redirect sends a 302 response to the browser. The browser then makes a new request to the new location. So it isn't a server side operation, and the browser can cache the response and not make the call again. Try appending a querystring parameter to make the browser do a true request. If you have fiddler available, you might use that to verify, but you are probably getting the page from cache.
protected void btnReturn_Click(object sender, EventArgs e)
{
Response.Redirect(String.Format("http://localhost/file_checker/?{0}", DateTime.Now.Ticks));
}

Intelligencia UrlRewriter PostBack Problem

i am using Intelligencia UrlRewriter for converting my page
www.mywebsite.com/subject.aspx?subject=sub1
to
www.mywebsite.com/subjects/sub1.aspx
On this page, I have next and previos buttons to browse the different subcategories on that subject and have used DataList with paging to support that.
When the page is first shown(IsPostBack=False) it works fine, but when next button is fired, the URL converts into this:
www.mywebsite.com/subjects/subject.aspx?subject=sub1
Is there any idea why it is happening ?
My web.Config file is as follows :
Mt web hosting company uses IIS 7.
EDIT: I have windows 7 and I tried by using local IIS and it ran fine there.
You can code this in your master page for this problem
Here form1 is the form tag and place it in master page's load event
protected void Page_Load(object sender, EventArgs e)
{
form1.Action = Request.RawUrl;
}

Categories

Resources