hello please help me for this question
i have the following url --> www.sample.com/news.aspx?id=45
i want pass "id" in the query string to news.aspx and show this news, but due to url rewriting the url is changed to this --> www.sample.com/news/my-news-45/
How to extract "id" from the query string?
Thanx for your help
you can manually done URL rewriting but The downside of manually writing code can be tedious and error prone. Rather than do it yourself, I'd recommend using one of the already built HttpModules available on the web for free to perform this work for you.
Here a few free ones that you can download and use today:
http://urlrewriter.net/
http://www.urlrewriting.net/149/en/home.html
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="rewriter"
requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" />
</configSections>
<system.web>
<httpModules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
</httpModules>
</system.web>
<rewriter>
<rewrite url="~/products/books.aspx" to="~/products.aspx?category=books" />
<rewrite url="~/products/CDs.aspx" to="~/products.aspx?category=CDs" />
<rewrite url="~/products/DVDs.aspx" to="~/products.aspx?category=DVDs" />
</rewriter>
</configuration>
The HttpModule URL rewriters above also add support for regular expression and URL pattern matching (to avoid you having to hard-code every URL in your web.config file). So instead of hard-coding the category list, you could re-write the rules like below to dynamically pull the category from the URL for any "/products/[category].aspx" combination:
<rewriter>
<rewrite url="~/products/(.+).aspx" to="~/products.aspx?category=$1" />
</rewriter>
the complete reference can be found on this linke
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
Related
I have a website developed in MVC 5 that perform search in inventory and the url is like this
http://localhost:56099/search/SQUARE
This url works, it redirects to Search controller and Index action with search query as SQUARE and gives the correct result. But, if I enter 2 dots as a search query it just takes me to my root page. The url will be like this
http://localhost:56099/search/..
it's strange because same thing works when passing single dot or multiple dots, so I can't find any technical reason why it is getting neglected.
I have done following things in Web.Config:
<modules runAllManagedModulesForAllRequests="true"> for accepting others characters also in search query.
relaxedUrlToFileSystemMapping="true"
But no success and I can't find any real reason for this weird behaviour. Any advice.
you should register the search route in your "RouteConfig.cs" file
routes.MapRoute(
"SearchRoute",
"Search/{*pathinfo}",
new { controller = "Search", action = "ActionName"});
You have the problem with .., because .. in your case is detected as relative URL, which indicates moving up to the parent directory(or root in your case), essentially stripping off everything up to the previous slash in the the Base URI.
UrlRoutingHandler in web.config should help you.
<system.webServer>
<handlers>
<add name="UrlRoutingHandler"
type="System.Web.Routing.UrlRoutingHandler,
System.Web, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
path="/Search/*"
verb="GET"/>
</handlers>
</system.webServer>
Then every URL starting with /Search will be considered as MVC URL.
Also you could try:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
I am using Intelligencia for my url-rewrite functionality.
<configSections>
<section name="rewriter" requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>
</configSections>
The below rule its working fine for my search page
<rewrite url="~/search/(.+).aspx" to="~/search.aspx?type=$1"/>
but i need to write my search page with below rule
<rewrite url="~/search/(.+)" to="~/search.aspx?type=$1"/>
If i run with above rule i am getting page not found error.
Plz advice me is there any setting in IIS.
Here is an article (with a downloadable solution sample) to use Routes to map to extensionless "SEO Friendly" urls:
http://goo.gl/BBnsa
I hope that you can help me with the below problem.
I am using ASP.NET MVC 3 on IIS7 and would like my application to support username's with dots.
Example: http://localhost/john.lee
This is how my Global.asax looks like: (http://localhost/{username})
routes.MapRoute(
"UserList",
"{username}",
new { controller = "Home", action = "ListAll" }
);
The applications works when I access other pages such as http://localhost/john.lee/details etc.
But the main user page doesn't work, I would like the app to work like Facebook where http://www.facebook.com/john.lee is supported.
I used below code and it didn't work for me at all:
<httpRuntime relaxedUrlToFileSystemMapping="true" />
I was able to use below code and get the app to accept dots but I definitely wouldn't like to use below code for many different reason, please tell me there is a way to overcome this problem.
<modules runAllManagedModulesForAllRequests="false" />
Add a UrlRoutingHandler to the web.config. This requires your url to be a bit more specific however (f.e. /Users/john.lee).
This forces every url starting with /Users to be treated as a MVC url:
<system.webServer>
<handlers>
<add name="UrlRoutingHandler"
type="System.Web.Routing.UrlRoutingHandler,
System.Web, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
path="/Users/*"
verb="GET"/>
</handlers>
</system.webServer>
Just add this section to Web.config, and all requests to the route/{*pathInfo} will be handled by the specified handler, even when there are dots in pathInfo. (taken from ServiceStack MVC Host Web.config example and this answer https://stackoverflow.com/a/12151501/801189)
This should work for both IIS 6 & 7. You could assign specific handlers to different paths after the 'route' by modifying path="*" in 'add' elements
<location path="route">
<system.web>
<httpHandlers>
<add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
</httpHandlers>
</system.web>
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</location>
I was facing the same issue. So the best solution for me is:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
<system.webServer>
For anyone getting an 'Cannot create abstract class' exception when using the UrlRoutingHandler approach, it's likely due to:
Using a restricted 'path' (e.g. path="/Files/*") in your web.config declaration, and
A folder/path with the same name exists in your project
I don't think the dot is the problem here. AFAIK the only char that should not be in the user name is a /
Without seeing the route that matches john.lee/details it's hard to say what's wrong, but I'm guessing that you have another route that matches the url, preventing the user details route from being matched correctly.
I recommend using a tool like Glimpse to figure out what route is being matched.
I am new to ASP.NET. I created a web application and decided to use url rewriting. I tried several solutions like http://urlrewriting.net and http://urlrewriter.net/index.php/support/configuration
These solutions worked fine on my localhost. But when I uploaded it to a shared hosting service provider, all my web pages get 500 internal server errors.
THe web hosting provider told me HttpModules and HttpHandlers are incompatible with IIS Integrated Pipeline mode. They said I'm supposed to move my settings into system.webServer...I tried doing that but must have messed up somewhere because I now get 404 errors. Can someone tell me how to get url rewriting to work for my scenario? Here's what my original web.config looks like:
<configSections>
<section name="urlrewritingnet"
restartOnExternalChanges="true"
requirePermission ="false"
type="UrlRewritingNet.Configuration.UrlRewriteSection,
UrlRewritingNet.UrlRewriter" />
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0"></compilation>
<httpModules>
<add name="UrlRewriteModule"
type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
</httpModules>
</system.web>
<urlrewritingnet
rewriteOnlyVirtualUrls="true"
contextItemsPrefix="QueryString"
defaultPage = "default.aspx"
defaultProvider="RegEx"
xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
<rewrites>
<add name="Rewrite" virtualUrl="^~/([^\/]+)/(\d+)$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/$1.aspx?id=$2"
ignoreCase="true" />
<add name="Rewrite" virtualUrl="^~/(search|administrator|Default|logout)$"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/$1.aspx"
ignoreCase="true" />
</rewrites>
</urlrewritingnet>
I think what they are saying is
1) You have to use the rewriter in .NET
2) You have to set it up to use the URL Rewriter, which sits under system.webServer, not system.web.
If I am correct, they are using the URL Rewriter: http://www.iis.net/download/urlrewrite
NOTE: They may not allow your custom HTTP handler (yes, I know it is a published third party, but ISPs are funny like that).
I am using urlrewriter.net
my intended url is like /articles/3/name_of_article/articles.aspx
& actual is articles.aspx?article =3;
(3 is just taken, it can be any number)
i am using regex like this
<rewriter>
<rewrite url="^/articles/(.+)/(.+)" to="/articles.aspx?article=$1" />
</rewriter>
it does not work, also if i delete module dll from references then also no exception is
thrown.
1) how can ensure that module is loaded (via code)?
2) is my regex correct?
my web.config contains this:
<configSections>
<section name="rewriter" requirePermission="false"
type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler,
Intelligencia.UrlRewriter" />
</configSections>
&
<httpModules>
<add type="Intelligencia.UrlRewriter.RewriterHttpModule,
Intelligencia.UrlRewriter" name="UrlRewriter" />
<add name="ScriptModule"
type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
2) is my regex correct?
No, you should probably change it to ^/articles/([^/]+)/.+$, otherwise the first capture will gobble up "3/name_of_article" and not just "3", and you don't need the second capture group. You can also write it with a non-greedy match in the capture group, e.g. ^/articles/(.+?)/.+$.
This doesn't answer your question at all, but thought you might like to know. I had used both UrlRewriter and ISAPI_Rewrite, and ISAPI_Rewrite was much better in my opinion.
You don't have to include any references or rewrite rules in your web.config. Rather you install it as an ISAPI extension in IIS and it has it's own config file, and the rules you write are almost identical to the Apache mod_rewrite module, so if you are familiar with that module it is another advantage.
Also, since it is executed at the web server level before being passed to the .NET framework, it doesn't need to be tied into the ASP.NET request life cycle.
You can check it out here.
ISAPI_Rewrite 3