i have a page with ~/x.aspx with urlmappings :
<add url="Home" mappedUrl="~/x.aspx" />
what i want is when calling ~/x.aspx?type=y then url still display Home
is there any way to do that
<add url="Home" mappedUrl="~/x.aspx" />
<add url="Home" mappedUrl="~/x.aspx?type=y" />
If you're using Web Forms, you can use the following tutorial. Basically the "type" could be a list of optional check boxes and the complete URL with parameters could be constructed in your code-behind.
Walkthrough: Using ASP.NET Routing in a Web Forms Application
For MVC, see the following question:
Routing with Multiple Parameters using ASP.NET MVC
I haven't work with mappings in the web.config but apparently is not possibly to use wildcards/regex
But you can do that by overwriting in your Global.asax the method Application_Start
protected void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// routing segment variable: {}
routes.MapPageRoute(null, "home", "~/Pages/x.aspx");
routes.MapPageRoute(null, "home/{type}", "~/Pages/x.aspx");
Related
I'm using Intelligencia.UrlRewriter to rewrite url in web form asp.net.
I have a patch that rewrites from url.com/page.aspx?id=10 to url.com/page/10/
To rewrite I'm using the following code:
<rewriter>
<rewrite url="~/page/([0-9]+)/?$*[/]" to="~/Page.aspx?Id=$1"/>
This method is working well, but here is a mistake:
When I try this path :
url.com/page/10/dada/asd/asda/da/sd/etc../
I will see contents of
url.com/page.aspx?id=10
And this is not good for seo.
i want this:
redirect from :
url.com/page/10/dada/asd/asda/da/sd/etc../
To
url.com/page/10/
How can I solve this?
Use Routing for this purpose:
First of all, create Global.asax file.
Add following code to the heading part of the Global.asax:
<%# Import Namespace="System.Web.Routing" %>
Then change Application_Start method to the following:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
And add RegisterRoutes method:
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "Page/{page_id}", "~/Page.aspx");
}
In your Page.aspx use the next code to read the route value:
if (Page.RouteData.Values["page_id"] != null){
//do anything what you need. For example, ShowPageValuesByPageID(Page.RouteData.Values["page_id"].ToString());
//ShowPageValuesByPageID is your method
}
How can I redirect www.example.com/foo to www.example.com\subdir\bar.aspx in IIS?
Note: a file called "foo" does not exist. I'd like to just redirect a URL with that pattern to the second URL
The best thing to do is not redirect, but instead use routing to map the URL to the web form. Then you keep a nice clean URL which is easy for users to type, and it looks better for search engines. We can accomplish that with the MapPageRoute method.
Add this to your Global application class (global.asax or global.asax.cs)
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "foo", "~/subdir/bar.aspx");
}
Alternatively, you can add this to your web.config to do a redirect.
<configuration>
<location path="foo">
<system.webServer>
<httpRedirect enabled="true" destination="/subdir/bar.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
How to get extension less and without query string SEO friendly URL for asp.net web forms.?
I have found out a very good article Here
It is a very good blog post written on how to redirect urls which contain query strings as extension less seo friendly urls.
One method of doing it by including Global.asax into the application.
Here is the example.
Include Global.asax into the application.
<%# Import Namespace="System.Web.Routing" %>
inside global.asax file
void registerroute(RouteCollection routes)
{
routes.MapPageRoute(
"Home-Route",
"Home",
"~/Default.aspx"
);
}
Which will map the home page or default page
For Query string urls like http://xyz.com/page.aspx?id=about
routes.MapPageRoute(
"Page-Route",
"Pages/{page}",
"~/page.aspx"
);
Then call this registerroute() inside application start event under Global.asax
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
registerroute(RouteTable.Routes);
}
Then to access the query string inside pages.
string pg = Page.RouteData.Values["page"] as string;
I'm using Crystal Reports in a Webform inside of an MVC application. Images in the reports are not being displayed, however, on both the ASP.NET Development Server and IIS 7 (on Win7x64).
I know from a number of other questions similar to this that the CrystalImageHandler HTTP Handler is responsible for rendering the image, but I've tried all of the usual solutions to no avail.
So far, I have
Added the following to my appSettings (via http://www.mail-archive.com/bdotnet#groups.msn.com/msg26882.html)
<add key="CrystalImageCleaner-AutoStart" value="true" />
<add key="CrystalImageCleaner-Sleep" value="60000" />
<add key="CrystalImageCleaner-Age" value="120000" />
Added the following httpHandler to system.web/httpHandlers (via https://stackoverflow.com/questions/2253682/crystal-report-viewer-control-isnt-loading-the-images-inside-the-report)
<add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=12.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
Added the following to my Global.asax.cs (via Crystal Reports Images and ASP.Net MVC)
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
and
routes.IgnoreRoute("CrystalImageHandler.aspx");
Any ideas as to why the images still 404?
I had similar problem. This helped me.
routes.IgnoreRoute("{*allaspx}", new { allaspx = #".*(CrystalImageHandler).*" });
I've tried the multitude of ways this can supposedly be made to work. None did. So I eventually settled on cheating:
public class CrystalImageHandlerController : Controller
{
//
// GET: /Reports/CrystalImageHandler.aspx
public ActionResult Index()
{
return Content("");
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
var handler = new CrystalDecisions.Web.CrystalImageHandler();
var app = (HttpApplication)filterContext.RequestContext.HttpContext.GetService(typeof(HttpApplication));
if (app == null) return;
handler.ProcessRequest(app.Context);
}
}
I added a route to this controller matching what Crystal expects (./CrystalImageHandler.aspx) and used this controller to invoke the handler when the action is executed. Not pretty, but functional.
Have you tried adding it to system.webServer/handlers? That should fix it on IIS7 but it is strange it doesn't work on the development server w/o that.
Add this in RouteConfig.cs file
routes.IgnoreRoute("Reports/{resource}.aspx/{*pathInfo}");
Note
"Reports" is the folder name which contains the aspx file of report viewer
change this folder name as per your application
i want to add some httphandlers for aspx pages by code via a http module.
is that possible? if it is, how?
thanks your advance..
Inherit IHttpModule, override Application_BeginRequest perform your rewrite logic and rewrite the URL with:
private void Application_BeginRequest(Object source, EventArgs e) {
((HttpApplication)source).Context.RewritePath(...);
}
Then register it in web.config with:
<httpModules>
<add name="UrlRewriteHandler" type="namespace.UrlRewriteHandler,project"/>
</httpModules>
Hope that helps.