Asp.net intelligencia UrlRewriter with 2 querystring parameter (hiding one of them) - asp.net

I'am using intelligencia.UrlRewriter on my web site.
I'am sending two querystring parameter to profil page.
<asp:HyperLink ID="lnkProfil" runat="server" Text='profil' NavigateUrl='/u/9f51e845-1089-4495-bd66-964db5b9c47b/tiju' ForeColor="Silver"></asp:HyperLink>
web config :
<rewrite url="~/u/(.+)/(.+)" to="~/Profil.aspx?user_id=$1&user_name=$2" />
and url seems like
http://yxyx.com/u/9f51e845-1089-4495-bd66-964db5b9c47b/tiju
But I want to make like
http://yxyx.com/u/tiju
or directly
http://yxyx.com/tiju (like facebook & twitter)
How can I hide user id parameter on url ?

If you want to have http://yxyx.com/u/tiju you should set it in the HyperLink
<asp:HyperLink ... NavigateUrl='/u/tiju' ...
In this case rewrite rule could be as
<rewrite url="~/u/(.+)" to="~/Profil.aspx?user_name=$1" />
As you see, in this case you will send only one parameter (tiju)

Related

Data URI is double-encoded by AntiXssEncoder

We have an ASP.Net Webforms (.Net 4.7.2) site. We've enabled the built-in XSS protection by adding to web.config:
<httpRuntime encoderType="System.Web.Security.AntiXss.AntiXssEncoder" ... />
This works fine, apart from in one place: we have some code that generates a small image, and embeds it within the page using a Data URI:
(aspx)
<asp:Image ID="image1" runat="server">
(C#)
image1.ImageUrl = dataURI;
and dataURI is normally something like:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMMAAADDAQMAAAA ...
This works fine without the AntiXssEncoder, but with that in place the rendered HTML turns into:
<img id="image1" src="data%3Aimage/png%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAA ...
... so the unsafe characters in the "header" of the src has been encoded, and the image doesn't display on the browser.
How can I disable the AntiXssEncoder for this one image object, or otherwise force the Data URI to get to the browser without being re-encoded? There is no user input on this particular page.
One way is to "do it yourself". Reference: https://stackoverflow.com/a/7406983/11534
Bascially
Declare a public property in your code-behind file with the image data. Let's say "ImageData" (public string ImageData {get;set;}) and set it to hold the base64 data.
Replace <asp:Image ID="image1" runat="server"> with <img src="<% =ImageData %>" />

Unable to redirect page in asp.net

I have grid on my asp.net page. grid having 3 columns with hyper links. both links working properly. but one is not redirecting desired page. I tried in different browsers as well . but got different error : as
In Mozilla Firefox :The page isn't redirecting properly
description In Mozilla Firefox :
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
* This problem can sometimes be caused by disabling or refusing to accept
cookies.
In Google chrome : `This webpage has a redirect loop`
description In Google chrome :
The webpage at http://myserver:425/(S(c0kr2xuftxiwhm25cm4vjg45))/mypage.aspx?type=2&userId=8 has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
since i tried lot of solutions over net , as i tried cleared cookies from browsers, added
<sessionState
cookieless="false" // True also tried
timeout="10">
</sessionState>
in web config file but problem is same there. I checked link is properly redirecting desired page. I mean i checked spelling of my link it is perfect. what else i have to try to resolve it ? where should i am making mistake ?
<ItemTemplate>
Go to my page
</ItemTemplate>
do check anywhere , are you redirecting same page ? I think you need to check each condition throughly , you must be redirecting same page either with same parameter or with different parameters.
Try adding runat="Server" attribute to the link tag, like this:
<ItemTemplate>
<a runat="Server" ID="linkID" href="mypage.aspx?type=2&userId=<%#Eval("userId") %>">Go to my page </a>
</ItemTemplate>
The most obvious place to check is inside mypage.aspx or mypage.aspx.cs for a redirect (see comment by paolo). The problem is not the link from the grid, this is simply a link.
What happens in this case is that you arrive at mypage.aspx, are redirected to mypage.aspx where you are redirected to mypage.aspx, etc.
You can also try this if you have one field in query string. Here in your case you have userid,
<asp:HyperLinkField HeaderText="Your Header"
DataNavigateUrlFields="userId"
DataTextField="Your Data Field to Display"
HeaderStyle-HorizontalAlign="Center"
DataNavigateUrlFormatString="mypage.aspx?type=2&userId={0}"
ItemStyle-Width="35%"
ItemStyle-HorizontalAlign="Left"
/>
In case of multiple
1) DataNavigateUrlFields="userId, employeeId, departmentid"
2) DataNavigateUrlFormatString="mypage.aspx?type=2&userId={0}&employeeId={1}&deptid={2}"
sequence of 1 and 2 must be same.

Routing to an anchor on another page

I am using Web Forms Routing in ASP.NET 4 and I am trying to route to a specific location on a page. On that page I have an element like <div id="3"> and I'd like to jump to this anchor from another page. For this purpose I have defined a Route in global.asax:
RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/{PageAnchor}",
"~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } });
The HyperLink to link to that page and the anchor "3" is defined this way in markup:
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=#3 %>">
Link</asp:HyperLink>
The problem with the generated link is that the # character in the URL gets encoded by %23 this way: http://localhost:1234/Path/SubPath/%233 so that I reach the target page but not at the specified anchor.
Is there a way to avoid this unwished URL-encoding? Or any other way to route to an anchor?
Thank you in advance!
Anchors are not supported with ASP.NET's routing feature. Routing is designed to support only the part of the URL after the application's path and before the anchor.
I suggest adding an event handler (e.g. Page_Load) and in that event handler generate the URL, append the anchor, and set the value on the HyperLink control.
Of course, in most cases with Web Forms routing it's easiest to just set the URL manually to whatever you want. This is a nice option when the URL is not complex and is unlikely to change.
Does this work?
RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/#{PageAnchor}",
"~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } })
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=3 %>">
Link</asp:HyperLink>
If you place the # outside of the PageAnchor placeholder, you could avoid that value being decoded, and it seems like a cleaner way to do it, besides.
How about the route goes to a controller and that reroutes to the page with the anchor parameter?
I know this is an old question, but maybe someone else could benefit from this approach.
Create a route without the anchor.
RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath", "~/MyPage.aspx");
And then construct the url like this, appending the anchor.
Link

Getting SharePoint absolute site url

I need to get the current absolute url of my site from inside an aspx page (no codebehind).
I tried $SPUrl, but it get's converted into a relative url.
<asp:Literal runat="server" text="<% $SPUrl:~Site/mypage.aspx %>" />
results in "/mypage.aspx". It's important that I get the full absolute url starting with "http://".
<asp:Literal runat="server" text="<% $SPUrl:~SiteCollection/mypage.aspx %>" />
does the same. The goal is to get this url: "http://myspweb.com/mypage.aspx" as result.
Jason is correct on how to get the URL.
One thing to realize however about SharePoint is there may not be an actual "true" absolute site URL. With a SharePoint content database, you can extend the application to different IIS applications which have different web.configs, etc. Often people do this when they have an extranet for employees (Active Directory security) but will then want to allow customers in via Forms Based Authentication. That is one of the main reasons that SharePoint converts things to a relative URL.
You could use SPContext.Current.Site.Url to get the absolute url of the current site collection:
<%# Import Namespace="Microsoft.SharePoint"%>
<!-- ... -->
<%= SPContext.Current.Site.Url %>

Dynamically set the DefaultValue of a ParameterBinding in a DataFormWebPart

In my custom aspx page in WSS I am using a DataFormWebPart with an XSL file to render some data. In order to pass values to the XSL I use parameter bindings. Specifically, I need to pass in the server host URL like this:
<ParameterBinding
Name="HttpHost"
Location="CAMLVariable"
DefaultValue="http://hardcoded.com" />
This works fine, but the next thing I want to do is to get the host name dynamically. So figuring out how to get that from SharePoint I added the following binding:
<ParameterBinding
Name="HttpHost"
Location="CAMLVariable"
DefaultValue='<%# SPContext.Current.Site.Url.Replace
(SPContext.Current.Site.ServerRelativeUrl, "") %>' />
Now to the problem. The code works as expected if used some other place in the page, but with the above code SharePoint reports:
Web Part Error: The 'ParameterBindings' property of 'WebPartPages:DataFormWebPart'
does not allow child objects.
Anyone have a take on this?
I have enabled server side code according to SharePoint 2007: using ASP.NET server side code in your pages
After trying various methods of manipulating the ParameterBindings property without success I thought of how I could get the dynamic value in there using the Location attribute.
The ParameterBinding Location attribute refers to where to fetch the value from. Articles like this hints of the "Control()" option. So changing the parameter binding to:
<ParameterBinding
Name="HttpHost"
Location="Control(MyHttpHost, Text)"
DefaultValue="" />
and adding the following code to my page:
<asp:TextBox ID="MyHttpHost" runat="server" Visible="false" />
<script runat="server">
protected void Page_Load()
{
MyHttpHost.Text =
SPContext.Current.Site.Url.Replace(SPContext.Current.Site.ServerRelativeUrl, "");
}
</script>
...actually did the trick!
To get to the parameter values from within the accompanying XSL file I put param elements in the root element. The param name attribute must match that of the ParameterBinding:
<xsl:stylesheet ...>
...
<xsl:param name="HttpHost"/>
The parameter can then be referenced as any other XSL variable.

Resources