HttpContext.Current.Request.Url.AbsolutePath with Master Page - c#

I am using HttpContext.Current.Request.Url.AbsolutePath as part of my security which the user needs to login before they access certain pages.
In this example and it works great. The user has to sign in before they view their profile.
if (Session["UserID"] == null)
{
Response.Redirect("Login.aspx/?ReturnURL=" + HttpContext.Current.Request.Url.AbsolutePath);
}
The end result looks like this:
http://localhost:54324/Login.aspx/?ReturnURL=/Profile_Page.aspx
The issue I am having is the pages are part of the master page. When the redirect occurs to the login page, nothing on the master page works. The navigation links do not fire, the images show broken links, etc. However when I access the Login Page directly everything in the master page works fine.

There's an extra forward slash in your redirect URL:
Response.Redirect("Login.aspx/?ReturnURL=" + HttpContext.Current.Request.Url.AbsolutePath);
^
Your login page still loads correctly because ASP.NET treats the slash as a separator for additional path information, similar to the way that a question mark is the separator for the query string.
But the extra slash causes a browser to resolve relative URLs for links and images relative to a child directory named Login.aspx instead of relative to the root of your application. For example, if you had the image <img src="Logo.png">, a browser would attempt to load Login.aspx/Logo.png. Removing the forward slash should fix the problem with the redirect:
Response.Redirect("Login.aspx?ReturnURL=" + Request.Url.AbsolutePath);
Nevertheless, visitors will still get broken URLs if they manually append the slash. To avoid this, use the built-in <asp:HyperLink> and <asp:Image> server controls, which will generate relative URLs taking the extra slash into account:
<asp:HyperLink runat="server" NavigateUrl="~/About.aspx" Text="About Us" />
<asp:Image runat="server" ImageUrl="Logo.png" />

Related

Having issue with using bookmark links with url routing

I have url routing setup on this site. I have a bookmark link in a nav that goes to a item in the home page like so:About . The problem is that, when someone entered the url incorrectly like site.com/contact/, instead of making the url site.com/default.aspx#about, it is making the url site.com/contact/default.aspx#about. All the other urls in the nav use the correct path. I know it has to do with the hashtag. Is there a way around it?
Instead of having
About
Try this:
<a href='<%= ResolveUrl("~/default.aspx#about") %>'>About</a>
This should always write the correct URL regardless.

Routing through 'a' tag in MVC Razor View

I am facing a redirect problem in my MVC application with 'a' tag. This is the code that i am using in my page
<li>Library</li>
<li>Computer Lab</li>
<li>Language Lab</li>
<li>Science Lab</li>
<li>Sports, Social & Cultural</li>
Here '/infra' is my Custom route and 'tabs-0,tab-1,...' all used to make some UI changes in the same page. Here my problem is for the first time when i am selecting an item like 'Computer Lab' then it will redirect to the same page, if i am selecting another menu item from the same page itself, then it will not redirect. still in the same page but i can see the change URL but cannot redirect
It's browser behaviar. If you have # sign in url and url is current page then it won't redirect. If you want to call controller then remove # sign.
In MVC you need to use the href link in the following manner.
href=<%:Url.Action("infra","tabs-0") %>
Try this one.
The "#" character is a special character to redirect to an anchor in your page. Don't use it in your case because you want to redirect to another page, not to scroll to a specific position in your page.
In your case the URL with the hashtag was correct because that was processed client-side by the browser.
try this
<li id="liTask" style="display: block;"><a href="" id="TaskUrl" ></a></li>
$('#TaskUrl').attr('href', '/infra/tabs-0');

Invalid ViewState when using jQuery tabs

I have a fairly simple page with a set of jQuery tabs, the content of some is called via ajax. I also have a search box in the masterpage in my header.
When I open the tabbed page the search box works fine. However once I have clicked on one of the ajax tabs the search box fails to work with an "Invalid Viewstate" yellow screen of death.
I believe this is because the ajax page is replacing the __VIEWSTATE hidden input with its own.
How can I stop this behaviour?
UPDATE: I have noticed that the YSOD only appears in IE and Chrome, Firefox doesn't seem to have the same issue. Although how the browser influences the ViewState, I'm not sure.
UPDATE: I've put a cut down version of the site that shows the issue here: http://dropbox.com/s/7wqgjqqdorgp958/stackoverflow.zip
The reason of such behavior is that you getting content of the ajaxTab.aspx page asynchronously and paste it into another aspx page. So you getting two instances of hidden fields with __VIEWSTATE name and when page posted back to server theirs values are mixing (might depends on how browser process multiple controls with same name on submit). To resolve this you can put second tab's content into a frame:
<div id="tabs">
<ul>
<li>Default Tab</li>
<li>ajax Content</li>
</ul>
<div id="tabs-1">
<p>
To replicate the error:
<ul>
<li>First use the search box top right to search to prove that code is ok</li>
<li>Then click the second ajax tab, and search again.</li>
<li>N.B. Chrome / IE give a state error, Firefox does not</li>
</ul>
</p>
</div>
<iframe id="tabs-2" src="ajaxTab.aspx" style="width:100%;" ></iframe>
</div>
Also, I'm not sure but this seems like error in the Web_UserControls_search control. In my opinion, NavBarSearchItemNoSearchItem_OnClick method must be refactored as below:
protected void NavBarSearchItemNoSearchItem_OnClick(object sender, EventArgs e)
{
var searchFieldTbx = NavBarSearchItemNo;
var navBarSearchCatHiddenField = NavBarSearchCatHiddenField;
var term = searchFieldTbx != null ? searchFieldTbx.Text : "";
if (term.Length > 0) //There is actually something in the input box we can work with
{
//Response.Redirect(Url.GetUrl("SearchResults", term));
Response.Redirect(ResolveClientUrl("~/Web/SearchResults.aspx?term=" + term + "&cat=" + navBarSearchCatHiddenField.Value));
}
}
Draw attention that we resolving client url when redirecting to search results page and instead of navBarSearchCatHiddenField use navBarSearchCatHiddenField.Value as cat parameter.
I guess that you use AJAX to fill the content of the tab. So in this case, content of your tab will be replaced by the new one from ajax and certainly _VIEWSTATE will be replaced. At server, do you use data from ViewState? In the "static tabs", you should prevent them auto reload by using cache:true
Your issue is that with your ajax call you bring in a complete ASPX page. Including the Form tag and its Viewstate. If you remove the Form tag from ajaxTab.aspx you will see everything works fine. asp.net does not know how to handle two Form tags in one page. Same goes for hidden Viewstate fields. You cannot bring in a full aspx page via ajax. Just bring in the content Div you want to display and you`ll be good to go.

How to set dynamic home page link in image logo?

MySite.com
when I click site logo, wanna appear sitefinity home page. how to set href atrribute in sitefinity as sitefinity CMS is dynamic home page?
there are several ways to achieve this, depending on how you have your logo setup on the page.
Your home page is usually setup to load when you visit the top level domain, which is at the root of your site.
If this is the case and you have your logo defined in a .master page, you can easily set the link to be the root, like
<img />
If, on the otherhand, you have the image on a sitefinity page or template, you need to make sure that you're adding the image inside a ContentBlock, and not the Image widget from the sidebar. This widget is only used to disaply an image, not link one.
by adding the image to the content block, you can then select it and add a link from the radeditor toolbar to the home page (or any other page)
hope this is helfpul!
Another, rather easy way of doing this would be using your url http://www.mysite.com as href for the logo, which is not too bad either (Although not as dynamic anymore)
But still helpful, especially if you are using some code in your print.css that looks like this:
a:link:after,
a:visited:after {
content: " (" attr(href) ") ";
font-size: 90%;
}
and renders href links after your actual links on printed pages.
This would result in something like the below:
[your company logo] http://www.mysite.com

HttpWebRequest reades only homepage

Hi I tried to read a page using HttpWebRequest like this
string lcUrl = "http://www.greatandhra.com";
HttpWebRequest loHttp = (HttpWebRequest)WebRequest.Create(lcUrl);
loHttp.Timeout = 10000; // 10 secs
loHttp.UserAgent = "Code Sample Web Client";
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();
Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream =
new StreamReader(loWebResponse.GetResponseStream(), enc);
string lcHtml = loResponseStream.ReadToEnd();
mydiv.InnerHtml = lcHtml;
// Response.Write(lcHtml);
loWebResponse.Close();
loResponseStream.Close();
i can able to read that page and bind it to mydiv. But when i click on any one of links in that div it is not displaying any result. Because my application doesnt contain entire site. So what we will do now.
Can somebody copy my code and test it plz
Nagu
I'm fairly sure you can't insert a full page in a DIV without breaking something. In fact the whole head tag may be getting skipped altogether (and any javascript code there may not be run). Considering what you seem to want to do, I suggest you use an IFRAME with a dynamic src, which will also hopefully lift some pressure off your server (which wouldn't be in charge of fetching the html to be mirrored anymore).
If you really want a whole page of HTML embedded in another, then the IFRAME tag is probably the one to use, rather than the DIV.
Rather than having to create a web request and have all that code to retrieve the remote page, you can just set the src attribute of the IFRAME to point ot the page you want it to display.
For example, something like this in markup:
<iframe src="<%=LcUrl %>" frameborder="0"></iframe>
where LcUrl is a property on your code-behind page, that exposes your string lcUrl from your sample.
Alternatively, you could make the IFRAME runat="server" and set its src property programatically (or even inject the innerHTML in a way sismilar to your code sample if you really wanted to).
The code you are putting inside .InnerHtml of the div contains the entire page (including < html >, < body >, < /html > and < /body> ) which can cause a miriad of problems with any number of browsers.
I would either move to an iframe, or consider some sort of parsing the HTML for the remote site and displaying a transformed version (ie. strip the HTML ,BODY, META tags, replace some link URLs, etc).
But when i click on any one of links in that div it is not displaying any result
Probably because the links in the download page are relative... If you just copy the HTML into a DIV in your page, the browser considers the links relative to the current URL : it doesn't know about the origin of this content. I think the solution is to parse the downloaded HTML, and convert relative URLs in href attributes to absolute URLs
If you want to embed it, you need to strip everything but the body part. That means that you have to parse your string lcHTML for <body....> and remove everything before and includeing the body tag. You must also strip away everything from </body>. Then you need to parse the string for all occurences of <a href="....."> that do not start with http:// and include h t t p://www.greatandhra.com or set <base target="h t t p://www.greatandhra.com"> in your head section.
If you don't want to embed, simply clear the response buffer and stream the lcHTML string back to the browser.
PS: I had to write all h t t p with spaces to be able to post this.
Sounds like what you are trying to do is display a different site embedded in your site. For this to work by dropping it into a div you would have to extract the code between the body tags as it wouldn't be valid with html and head in the middle of another page.
The links won't work because you've now taken that page out of context in your site so you'd also have to rewrite any links on the page that are relative (i.e. don't start with http) to point to a page on your site which will then fetch the other sites page and display them back in your site, or you could add the url of the site you're grabbing to the beginning of all the relative links so they link back to that site.

Categories

Resources