Two parameters with custom URL. Rewrite in IIS, can't figure out syntax - iis-7

I've set this up and it works just fine
https://www.iis.net/learn/extensions/url-rewrite-module/using-custom-rewrite-providers-with-url-rewrite-module
But what I want to do is alter the stored procedure that fires off so that I can have more parameters. In their sample rule, you end up with this
<rule name="DbProviderTest" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{DB:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" />
So on the page I set a new variable that I called ENVIRON. What I hoped to do was have different rules for test and production. So I modified the RewriteTable to include a column for "environment".
What has me stumped is that I can't figure out how to pass two parameters to the stored procedure. I thought it would be as simple as something like this
<add input="{DB:{R:1},{ENVIRON}}" pattern="(.+)" />
I tried numerous other syntax but I keep getting the same 500.x error:
Procedure or function 'GetRewrittenUrl' expects parameter '#env', which was not supplied
So my question is simple, when creating this setup, how does one pass more than a single parameter to the stored procedure?

As per Microsoft Documentation: "The provider connects to a SQL Server database and executes a stored procedure that takes a NVARCHAR input parameter containing the input URL string and returns a one row, one column result set containing the output URL string of NVARCHAR type."
Unfortunately what you are trying to achieve is not possible as such stored procedures can only accept a single parameter (which must be called #input).

So you can't use a second parameter, however, you can replace {R:1} with a delimited parameter to the database.
ie. {DB:{R:1}} could become {DB:{HTTP_HOST}/{R:1}}
Then you can alter your stored procedure to handle that.

Related

IIS 7 rewrite rule - rewrite same page, one or multiple query string parameters

I have the following rewrite rules setup
<rule name="Department Rewrite" stopProcessing="true">
<match url="^products/([^$]+)" />
<action type="Rewrite" url="products?durl={R:1}" />
</rule>
<rule name="Product Rewrite" stopProcessing="true">
<match url="^products/([^$]+)/([^$]+)" />
<action type="Rewrite" url="products?durl={R:1}&purl={R:2}" />
</rule>
The Department Rewrite works as expected and now I'm trying to configure the Product Rewrite to work.
With the URL products/department
Request.QueryString["durl"] outputs department
With the URL products/department/product
Request.QueryString["durl"] outputs department/product
Request.QueryString["purl"] is NULL
How do I go about getting Product Rewrite to work as expected?
UPDATE
Also looking at how to ignore the URL rewrites if they contain action/edit. For example
products/department/action/edit
products/department/product/action/edit
Assuming I need to apply some conditional statements but unsure how to write them.
What is the [^$] trying to do?
In the department rewrite, the first group is saying "matching anything but a $", which is the rest of the url, so that rule applied, and it's all in durl.
Try these, which are saying "match anything but a slash". I also like putting a $ to end it, if you know that's the full url (it can still have querystring parameters). That will make the product skip past the department rule.
In department rule:
<match url="^products/([^/]+)$" />
In product rule:
<match url="^products/([^/]+)/([^/]+)$" />
I like the consistency that both groups are the same, but you could make the second one take anything, for a simpler rule:
<match url="^products/([^/]+)/(.+)" />
A side suggestion: put the more-specific rules first, so if you send in just one level (department), it definitely won't match the product rule. The way it is now, the department grabbed the product rule. If they were reversed, it probably would have worked, even without updating the rules.

asp.net url rewrite based on part of url?

Currently my urls look like this:
www.mysite.com/venues/<venueid>/<venuename>
e.g.
www.mysite.com/venues/342/myvenue
it's quite easy creating a rewrite url for this by checking for the occurence of "venues"
<rule name="view venue">
<match url="^venues/([0-9]+)?/??([a-zA-Z0-9-]+)?$" />
<action type="Rewrite" url="locationdetails.aspx?id={R:1}" />
</rule>
But for a NEW site I want my URLS to look like this:
www.mysite.com/<venuename>-<venueid>
e.g.
www.mysite.com/myvenue-342
I though of creating rewrite rules that check on a number in the url and use that as the venue id, but that would cause an issue if the venue venuename and this friendlyurl would contain a number, e.g. "pier 39" would then become "pier-39-342", or is there a way to capture the last id in the friendly url (so the id after the last dash) and use that as the parameter for R:1?
I already looked at this blog: http://weblogs.asp.net/owscott/archive/2010/04/20/url-parts-available-to-url-rewrite-rules.aspx which seems to cover my needs, but I have no idea how to use it.
This rule seems to do what I need:
<rule name="venue details">
<match url="^([a-zA-Z0-9-]+)-([0-9-]+)$" />
<action type="Rewrite" url="locationdetails.aspx?id={R:2}&furl={R:1}" />
</rule>

Getting list of key values from app config with same name

I have follwing in app.config file:
<appSettings>
<add key="Name" value="Office"/>
...
<add key="Name" value="HotSpot"/>
...
<add key="Name" value="Home"/>
</appSettings>
I tried
ConfigurationManager.AppSettings["Name"]
But it only gives me one Value? How can i get list of all values? I am using c# 3.5. Is there lambda expression or something i can use to get that?
You can only use one key per value, so this approach will not work.
There are two alternate approaches I can think of:
Use a single key with a delimiter, and retrieve with ConfigurationManager.AppSettings["Name"].Split(new [] { "," });.
<add key="Name" value="Office,Hotspot,Home" />
Use a custom section to create a section that can contain your array of strings.

How to do URL rewriting for the URL to hide filename.aspx and querystring?

https://mysite.com/Admin/Dashboard/DashboardReport.aspx - this is my actual URL i want to hide the filename.aspx with if any querystring values. how?
also i have tried FriendlyURL in URL rewriting in IIS.
it doesn't take effect.
Pls give me a quick reply.
You want to use rewrite rules and the rewrite module.
http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/
URL Rewriting is somewhat ambiguous. Are you rewriting what the user sees? Or what the server sees? It's better to believe that you are not able to alter what the URL displays to the website consumer.
However, the trick is to send them to a broken, not-real URL... then ReWrite the URL for YOU, so you know what that not-real URL means. For example:
Imagine you have a Real URL like so:
http://www.example.com/folder/subfolder/page.ext?param=value
But you want to "Rewrite" the URL to look like so:
http://www.example.com/subfolder/value
If they are on the page page.ext..., you cannot change what they see in their URL bar. But, you can Redirect Them to a fake path /subfolder/value like so:
<rule name="subFolder Value Redirect">
<conditions>
<add input="{REQUEST_URI}" pattern="^/folder/([^/]+)/page\.ext\?param=([^&]+)$" />
</conditions>
<action type="Redirect" url="/{C:1}/{C:2}" />
</rule>
Above, I am not doing a "url match" because I am depending solely on a Condition. My only Condition is that the Request_URI looks exactly like I expect. Some folder, some subfolder name, page.ext, and a querystring with param=SomeValue. I use () parenthesis to dynamically capture the Subfolder Name and the param Value.
Note: {C:0} means the "0th" Capture Group of the Conditions. Capture 0 is the Entire Result of the regex ^/folder/([^/]+)/page\.ext\?param=([^&]+)$. Capture 1 is the First Capture Group, the Subfolder Name. Capture 2 is the second Capture Group, the param Value.
^ at the beginning of a regex means "starting from the beginning". Sometimes you don't want to start at the beginning and that wouldn't be there.
$ at the end of a regex means "ending at the end". Sometimes you don't want to end at the end and that wouldn't be there.
The Character Class [^...] means to capture any character that is not .... In this case, capture anything that is not a Slash, or anything that is not an Ampersand.
The repetition operator [...]+ Plus-Symbol means to capture the preceding rule at least once, but up to many times.
. is a special character which means "anything". But we mean it as in the dot of an extension page.ext. So you escape it like \.
{REQUEST_URI} means to compare your pattern against the entire URL after the Domain/Port. So in the URL http://www.example.com:1234/subfolder... The /subfolder and all following contents are in the {REQUEST_URI} EXCEPT hashtag routing. So if you have an Anchor or Angular UI Route, those stay with the browser only, and will always be at the end of any redirect, but the server will never see them, so you can't parse off of hashtags.
action type="Redirect" just means this action is to redirect the user to a given URL.
url="/{C:1}/{C:2}" is the URL relative to the domain, since it doesn't start with a protocol. This will redirect to /subFolder/value, because Condition capture 1 and Condition capture 2 in our pattern capture those two values.
After inserting this rule, a user will be redirected to a broken janked up URL that looks like a State-Representative URL (RESTful). Yaay!!
URL Rewriting
The rewrite is for you (the server), not for the customer. But now that they are redirected to a nice pretty broken URL, you can find the context of that URL and tell your server what it REALLY means by rewriting it for the server.
<rule name="subFolder Value Rewrite">
<conditions><add input="{REQUEST_URI}" pattern="^/([^/]+)/([a-zA-Z0-9]+)$" /></conditions>
<action type="Rewrite" url="/folder/{C:1}/page.ext?param={C:2}" />
</rule>
This is pretty much the inverse of the prior rule. This finds a pattern where there is a path "subFolder" name, slash, and a value made up of letters and numbers. It also, once again, describes the Begginning ^ and End $, because we don't expect unexpected weird things. So, this will catch a URL like:
http://www.example.com/subFolder/1234abcDE
But will not catch
http://www.example.com/lol/somethingelse/15?blah=kthx
The rewrite URL then evaluates back to a true real URL that maps to a path for processing for a file that really exists. And, keep in mind, that "rewrite" means it takes the "incorrect" fake value that the user sees, and rewrites it back to an ugly "correct" value for the server. Not the other way around.

debugging Url rewriting in umbraco

I have a url rewrite rule specified for my umbraco site like:
<add name="circuitdetail"
virtualUrl="^~/circuits/(.*).aspx"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/circuits/detail.aspx?circuit=$1"
ignoreCase="true" />
I would like the /circuits/albert-park to be mapped to /circuits/detail?circuit=albert-park.
When I enter that second url I correctly get the desired page but when I enter the first, I get my 404 page.
In Umbraco I have a page named "Circuits" and nested underneath it is the page named Detail. As described above, entering the unfriendly url works but it seems the url rewrite is not working.
What am I doing wrong?
It looks as if your RegEx is matching the destinationURL as well as the VirtualURL.
So when it re-writes to ~/circuits/detail.aspx?circuit=$1
This is also being matched by the ^~/circuits/(.*).aspx RegEx.
Adding a $ to the end of your RegEx should fix it. The $ tells the RegEx to match up to the end of the line.
i.e.
^~/circuits/(.*).aspx$
More info: http://www.regular-expressions.info/anchors.html

Resources