Having issue with using bookmark links with url routing - c#

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.

Related

HttpContext.Current.Request.Url.AbsolutePath with Master Page

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" />

Routing in MVC doesn't always work correct

My problem is as simple as the title.
I want to link to an action from a controller, but doesn't always work. I know what the problem is, but don't know how to solve it.
Imagine you're at the home page, the URL is then something like this: localhost:1234/. When I use the following URL in a link (<a>-tag) to go to the action configure, the href in that link-tag will then look like this: configure. But this doesn't work because it will send the user to localhost:1234/configure. Instead of this it should be localhost:1234/device/configure where device is the controller.
I could change the href in the link-tag to device/configure, but then it wouldn't work anymore when the user is redirected to the homepage. Because the url of the homepage is then localhost:1234/device/view (the default route, configured in RouteConfig.cs) and the link will send the user to localhost:1234/device/device/configure
I've already tried to use #Url.Action and #Html.ActionLink, but that doesn't work either.
Does anyone know how to make sure it will always send the user to the right URL?
Here is my RouteConfig if you need it.
Try this:
#Html.ActionLink("link text", "configure", "device")

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');

MSCRM Sitemap link to .htm page. How do I pass parameters?

I have a Site Map link that opens up a Web Resource .htm page. Everything works great except I want to pass some values to that page. I assumed I could pass them as a querystring since there is the Xrm.Page.context.getQueryStringParameters() method but my querystring seems to get stripped out.
<SubArea Icon="$webresource:my_webresourceicon.gif"Id="nav_my_webresourceid" Url="$webresource:my_webresourcepage.htm?xyz=123" Client="All" AvailableOffline="false" Title="My Web Resource Page" Description="My Web resource Description" Sku="All" PassParams="true"/>
I want to be able to access the parameter xyz from my_webresourcepage.htm as the page loads. What is the best way to go about passing this value?
Thanks
Unfortunately you can't pass xyz unless you modify the registry. Instead, you want to use the parameter "data".
http://msdn.microsoft.com/en-us/library/gg309536.aspx

Explicitly treat link as an absolute link?

I have a control on one of my page which takes some user input (URL) and allows the user to test the link.
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("URL") %>' Target="_blank">Click here to test link</asp:HyperLink>
The problem is if the user puts in the URL "google.com", then the link will be treated as "http://localhost/google.com". I have to put "http://www.google.com" for it to go to the right place.
Is it possible to treat the link like it is an absolute link in all cases?
Try this...
NavigateUrl='<%# Eval("URL", "http://{0}") %>'
Edit: if you want to add check whether it already contain http:// then it should be like..
NavigateUrl=<%# Eval("URL").ToString().Contains("http://") == true ? Eval("URL") :
"http://" + Eval("URL") %>
Is this question of academic nature or do you have a problem to solve?
Firstly, the notion that http://www.google.com is the right place and http://localhost/google.com is incorrect is very wrong.
Your problem as I see is this.
When you pull urls from a datasource there are raw urls which ASP.NET Framework recognizes as relative URL. But you would like them to use their absolute uri.
But the problem is not with the framework as http://localhost/google.com could be a valid URL.
A good example for the same will be the site builtWith. It has valid URLS like http://builtwith.com/facebook.com
And so, the problem is at the place where you have input the data. Why could you not place a small combo so that users could easily add the protocols and then append the url to it as shown below?
Hope this helps.
Based on your comments:
What if it's a secure (https) site or an FTP site?
and
Would that work? What if the input already has http in it? What if it's a secure (https) site or an FTP site?
and
The problem is if the user puts in the URL "google.com", then the link will be treated as "http://localhost/google.com". I have to put "http://www.google.com" for it to go to the right place.
Is it possible to treat the link like it is an absolute link in all cases?
You're asking for magic. We don't specialize in magic here. You have to specify something that lets you know that it can become a specific type of domain. Tell them if they don't specify the protocol you will as http:// and the rest of the time it's up to them to specify the protocol. It's all you can do.
As for determining if it's got a protocol, check string.Contains("://"), since that's what the browser is going to do anyway, more or less.
In your code-behind, check if the user input contains "http://" and append it if it doesn't. You can also use JavaScript to do this.

Categories

Resources