0

In my Asp.net 4.0 website, i have a page that shows the profile of particular user. The page url is given below.

www.mysite.com/PublicProfile.aspx?UserID=1234

I want to setup such a mechanism that the server redirect or provide the response if client provides below request

www.mysite.com/1234

I think it can be done by url rewriting or using the Http handlers. Any help on this, thanks in advance.

LojiSmith
  • 282
  • 3
  • 20
  • A more involved alternative to url rewrite especially if you're going to be doing this a lot is to implement mvc routing in web forms: http://weblogs.asp.net/scottgu/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series – RandomUs1r Mar 25 '15 at 17:18
  • This might help: http://www.asp.net/web-forms/videos/net-4/routing/how-do-i-use-routing-with-aspnet-web-forms – HerbalMart Mar 25 '15 at 17:27
  • I might suggest that you modify your routing slightly to `www.mysite.com/users/1234`. It makes it more clear what the ID number is for, and leaves open the possibilities of future URL's like `www.mysite.com/articles/1234`. The quick and dirty way to do this is to implement [Friendly URL's](http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx). That allows you to drop the file extension from the URL and use segments instead of Query String for obtaining values from the URL. – mason Mar 25 '15 at 17:34
  • If you do this, make sure things work the way they should if you change the user ID to 1235, a bunch of letters, an invalid value, or nothing. (URLs can be modified) – Tim Mar 25 '15 at 17:49

2 Answers2

1

You can find here an awesome article about url rewrite and the result you want will look like below.

<rule name="PublicProfile" stopProcessing="true">
   <match url="^([0-9]+)$" ignoreCase="true"/>
   <action type="Rewrite" url="/PublicProfile.aspx?UserID={R:1}"/>
</rule>

Notes:

  1. ^([0-9]+)$ for numbers only
  2. stopProcessing="true" means that no more rules will be checked.
  3. {R:1} will contain the number that matched the rule
  4. The rule need to be added in Web.config under <system.webServer><rewrite><rules> <!-- add rule here --> </rules><rewrite><system.webServer>
adricadar
  • 9,971
  • 5
  • 33
  • 46
  • thanks, but in the config file it doesn't accept the tag. do i need any reference? – LojiSmith Mar 26 '15 at 07:44
  • @LojiSmith look at this for [making rewrite rule to work](http://stackoverflow.com/questions/363231/asp-net-url-rewrite-module-and-web-config) – adricadar Mar 26 '15 at 11:00
-2

Yes, you are write it is possible with url rewriting.

You need to create a rewrite rule by using URL Rewrite UI in IIS Manager The steps are:

Go to IIS Manager.
Select Default Web Site.
In the Feature View click URL Rewrite.
In the Actions pane on the right-hand side, click Add rules.
In the Add Rules dialog box, select Blank Rule and click OK.

Now you must define the actual rewrite rule. In the URL Rewrite Module, a rewrite rule is defined by specifying four required pieces of information:

Name of the rule. Pattern to use for matching the URL string. Optional set of conditions. Action to perform if a pattern is matched and whether all conditions checks succeed.

NAMING A RULE

In the Name text box, enter a name that will uniquely identify the rule, for example: ”Rewrite to article.aspx”.

DEFINING A PATTERN

In the Pattern text box, enter the following string: ^article/([0-9]+)/([_0-9a-z-]+)

This string is a regular expression that specifies that the pattern will match any URL string that meets the following conditions:

Starts with the sequence of characters “article/”. Contains one or more numeric characters after the first “/”. Contains one or more alphanumeric or “_” or “-” characters after the second “/”.

Notice that certain parts of the regular expression are within parentheses. These parentheses create capture groups, which can be later referenced in the rule by using back-references.

DEFINING AN ACTION

Since the rule that we are creating is supposed to rewrite the URL, choose the Rewrite action type that is listed in the Action group box. In the Rewrite URL: text box, enter the following string: article.aspx?id={R:1}&title={R:2}

This string specifies the new value to which the input URL should be rewritten. Notice that for the values of the query string parameters we used {R:1} and {R:2}, which are back-references to the capture groups that were defined in the rule pattern by using parentheses.

Leave default values for all other settings. The Edit Inbound Rule property page should look atteched image Here

VIEWING THE REWRITE RULE IN CONFIGURATION FILE

The rewrite rules are stored either in the ApplicationHost.config file or in Web.config files. To check the configuration of the rule that we have just created, open a Web.config file located in %SystemDrive%\inetput \wwwroot. In this file you should see the section that contains this rule definition:

 <rewrite><rules>
<rule name="Rewrite to article.aspx">
  <match url="^article/([0-9]+)/([_0-9a-z-]+)" />
  <action type="Rewrite" url="article.aspx?id={R:1}&amp;title={R:2}" />
</rule>

</rewrite>

TESTING THE REWRITE RULE

To test that the rule correctly rewrites URLs, open a Web browser and request the following URL:

http://localhost/article/234/some-title

You should see that the rewrite rule on your Web server has changed the original URL to Article.aspx and it has passed “234” and “some-title” as values for query string parameters.

  • Don't just link to off site resources. Your answer should be able to stand on its own merit without the link. You can either delete your answer, or copy the relevant info from the link into your answer. – mason Mar 25 '15 at 17:49
  • can i share any link from stackoverflow or Microsoft(MSDN)? – Sunil Kumawat Mar 25 '15 at 17:52
  • If the link is to Stack Overflow, it should just be a comment or you can flag the question as a duplicate of that post. If the link is to MSDN, it should just be a comment *or* summarize enough info from the link into your answer that it's actually useful if the link were to break. You shouldn't ever just have an answer that says "look at this link", whether the link is to SO or MSDN. – mason Mar 25 '15 at 17:54
  • already above a member shared a link from other site with his answer so here i am using this. – Sunil Kumawat Mar 25 '15 at 17:55
  • He shared the link to another site (which is fine) **and** he summarized the relevant information into his answer. You have not summarized the relevant information from the link into your answer, which is why you should delete it or update it. – mason Mar 25 '15 at 17:57
  • Yes, but he also included code examples and helpful information. This could be construed as a link only answer, which is not a good answer. Read [How To Answer](http://stackoverflow.com/help/how-to-answer) – Jake Mar 25 '15 at 17:59
  • I have lost my 2 points from my reputation, is there any way to get this back, i mean to say by deleting above answer or other process? – Sunil Kumawat Mar 25 '15 at 17:59
  • @SunilKumawat I recommend going through to the help center and reading the topics there. Starting with the one I linked to you in a previous comment. – Jake Mar 25 '15 at 18:06