TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Tridip Bhattacharjee
NA
1.2k
78.2k
ASP.Net WebForm: rewrite rule is not working in my web.confi
Nov 16 2017 2:26 PM
i am using webform and my .net framework version is 4.5. i need to inject country code in url. i could inject it from global.asax file.
here is the code
protected
void
Application_BeginRequest(Object sender, EventArgs e)
{
string
rawUrl = HttpContext.Current.Request.RawUrl;
//check for 2 characters wrapped in forward slashes eg. /en/
string
countryCode = Utility.GetCountryCodeFromUrl(rawUrl);
SiteCulture Culture = SiteGlobalization.DefaultCulture;
if
(
string
.IsNullOrEmpty(countryCode))
{
//check cookie
if
(!
string
.IsNullOrEmpty(Utility.GetCookieValue()))
{
string
threeDigitIsoRegionCodeCookie = Utility.GetCookieValue();
Culture = SiteGlobalization.BBACultures.Values.FirstOrDefault(x => x.ThreeDigitISORegionCode.Equals(threeDigitIsoRegionCodeCookie, StringComparison.InvariantCultureIgnoreCase));
if
(Culture !=
null
&& !
string
.IsNullOrEmpty(Culture.TwoDigitISORegionCode))
{
countryCode = Culture.TwoDigitISORegionCode;
}
}
//check the referer.
if
(
string
.IsNullOrEmpty(countryCode) && HttpContext.Current.Request.UrlReferrer !=
null
&& !
string
.IsNullOrEmpty(HttpContext.Current.Request.UrlReferrer.AbsolutePath))
{
countryCode = Utility.GetCountryCodeFromUrl(HttpContext.Current.Request.UrlReferrer.AbsolutePath);
}
//finally browser
countryCode =
string
.IsNullOrEmpty(countryCode) ? Utility.Get2DigitCountryCodeFromBrowser().ToLower() : countryCode;
string
redirectUrl =
string
.Format(
"/{0}{1}"
, countryCode, rawUrl);
HttpContext.Current.Response.RedirectPermanent(redirectUrl.ToLower());
}
/* is the country code valid ? */
Culture = SiteGlobalization.BBACultures.Values.FirstOrDefault(x => x.TwoDigitISORegionCode.Equals(countryCode, StringComparison.InvariantCultureIgnoreCase));
if
(Culture ==
null
||
string
.IsNullOrEmpty(Culture.ThreeDigitISORegionCode))
{
HttpContext.Current.Response.RedirectPermanent(
string
.Format(
"/{0}/index.aspx"
, SiteGlobalization.DefaultCulture.TwoDigitISORegionCode.ToLower()));
}
if
(HttpContext.Current.Request.FilePath.EndsWith(
"/"
+ countryCode))
{
HttpContext.Current.Response.RedirectPermanent(
string
.Format(
"/{0}/index.aspx"
, countryCode.ToLower()));
}
/* set the default cutlure */
Utility.SetCountry(Culture);
}
the above code is working and injecting country code as per cookie value. so when i run my project from VS2013 IDE then url look like http://localhost:53741/gb/ or http://localhost:53741/gb/default
but getting error when browsing from IDE. because there is no folder called gb in my project.
so i add rewrite rule and that is as follows
<
rewrite
>
<
rules
>
<
rule
name
=
"Rewrite language code"
>
<
match
url
=
"^([a-z]+)/([0-9a-z]+).aspx"
/>
<
action
type
=
"Rewrite"
url
=
"/{R:2}.aspx?lang={R:1}"
/>
</
rule
>
</
rules
>
</
rewrite
>
my full web.config code as follows with rewrite rule
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?
LinkId
=
169433
--
>
<
configuration
>
<
system.web
>
<
compilation
debug
=
"true"
targetFramework
=
"4.5"
/>
<
httpRuntime
targetFramework
=
"4.5"
/>
<
pages
>
<
namespaces
>
<
add
namespace
=
"System.Web.Optimization"
/>
</
namespaces
>
<
controls
>
<
add
assembly
=
"Microsoft.AspNet.Web.Optimization.WebForms"
namespace
=
"Microsoft.AspNet.Web.Optimization.WebForms"
tagPrefix
=
"webopt"
/>
</
controls
>
</
pages
>
</
system.web
>
<
runtime
>
<
assemblyBinding
xmlns
=
"urn:schemas-microsoft-com:asm.v1"
>
<
dependentAssembly
>
<
assemblyIdentity
name
=
"Newtonsoft.Json"
culture
=
"neutral"
publicKeyToken
=
"30ad4fe6b2a6aeed"
/>
<
bindingRedirect
oldVersion
=
"0.0.0.0-6.0.0.0"
newVersion
=
"6.0.0.0"
/>
</
dependentAssembly
>
<
dependentAssembly
>
<
assemblyIdentity
name
=
"WebGrease"
culture
=
"neutral"
publicKeyToken
=
"31bf3856ad364e35"
/>
<
bindingRedirect
oldVersion
=
"0.0.0.0-1.5.2.14234"
newVersion
=
"1.5.2.14234"
/>
</
dependentAssembly
>
</
assemblyBinding
>
</
runtime
>
<
system.webServer
>
<
rewrite
>
<
rules
>
<
rule
name
=
"Rewrite language code"
>
<
match
url
=
"^([a-z]+)/([0-9a-z]+).aspx"
/>
<
action
type
=
"Rewrite"
url
=
"/{R:2}.aspx?lang={R:1}"
/>
</
rule
>
</
rules
>
</
rewrite
>
</
system.webServer
>
</
configuration
>
probably something i have missed for my rewrite rule for which it is not working. so any one can tell me what is mistake in my code or in my rewrite rule ?
i am running site from VS2013 IDE and that is why i have not installed IIS rewrite module for IIS.
Reply
Answers (
0
)
How to compare two tables for login
ASPX extension not showing when browing site