how to show different url in C#? - c#

How can i change view like "www.abc.com/welcome" in browser but actual path is "www.abc.com/welcome.aspx".
And when i type "www.abc.com/welcome" then will go path "www.abc.com/welcome.aspx" but still view like "www.abc.com/welcome".
I have try this code on web.config below but got error:Unrecognized configuration section urlMappings
<urlMappings enabled="true">
<add url="~/welcome.aspx" mappedUrl="~/welcome" />
</urlMappings>
I wonder still got other way?

Where did you get the information about this urlMappings section? It's not supported by default by IIS or ASP.Net.
I think you might want to look at the UrlRewrite Module.
With this it's trivial to setup url rewrites like the one you want.

If you're using a URL rewriting module, you need to make sure which version of IIS you will be running on before something like "/welcome" will work. IIS6 doesn't support extensionless URLs by default. You'll need to have an ISAPI filter running for it, or you'll need to run on IIS7.

I suggest you to use routing How to: Use Routing with Web Forms.
You will need to register the UrlRoutingModule and the UrlRoutingHandler handler to be able to use routing feature (more details could be found in the article above).
And then in global.asax
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add("BikeSaleRoute", new Route
(
"bikes/sale",
new CustomRouteHandler("~/Contoso/Products/Details.aspx")
));
}

Related

URL rewriting in asp.net 3.5 withiout IIS

I am working in as asp.net application which is in asp.net 3.5 version. I have a requirement to implement URL rewriting. I have defined 4 pages like
www.abc.com/page1.aspx
www.abc.com/page2.aspx
www.abc.com/page3.aspx
www.abc.com/page4.aspx
I want that when user types www.abc.com/language1 then
www.abc.com/page1.aspx open. If user types www.abc.com/language2, then www.abc.com/page2.aspx should open.
Please suggest a solution to it.
Also, as this site is complete and have links sent through email to users ( and some of the links have querystrings) what is best way to redirect users to new urls without querystrings and generate new links using new pattern ?
I have gone through followig techniques:
http://www.iis.net/downloads/microsoft/url-rewrite ( this is with IIS, can i use it with asp.net 3.5 and without IIS ? )
ASP.NET URL Rewriting in very easy way ( it is not tested for security issues)
http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx ( This require existing links to change ? )
Please suggest
public class Global : System.Web.HttpApplication
{
void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route("language2", new PageRouteHandler("~/page2.aspx")));
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
}
You can do this under Application_BeginRequest in Global.asax file, Check If Request comes from specified URL, then Redirect user to another Page, eg : if(Request.Url.ToString().ToLower().Contains("language1.aspx") , then Response.Redirect("Page1.aspx") .

Querystring without argument name

I am trying to get this to work. I have a DNN module in which I read from a querystring and perform a few steps. All of that is working fine. Now I am trying to clean up the URL while reading the querystring
Right now, the URL looks something like this:
http://mysite.website.com/?pid=1234
I would like it to look like:
http://mysite.website.com/1234
Is something like this even possible?
You are much better to use a proper rewriting solution for DotNetNuke (e.g. iFinity UrlMaster and there are others...).
You can then write a custom url provider for your module.
That's what I've done on my site to rewrite parts of my articles module (e.g. www.ventrian.com/blog/
You can find more information about urlmaster here:
http://www.ifinity.com.au/Products/Url_Master_DNN_SEO_Urls
look at using a URL Rewriter module. There are several third party ones for IIS6, but Microsoft provides one for IIS7 and IIS7.5. You basically configure it with a regular expression and change the output.
Microsoft's rewrite module for IIS7 is available at: http://www.iis.net/downloads/microsoft/url-rewrite
You've got a couple of choices:
Explore the rewrite capabilities available in DNN and how to use them. They can be found in Host Settings > Advanced Settings > Friendly URL Settings. Or use the 2nd option based on which version of IIS you're working on.
2a. URL Rewrite Module for IIS 7 & above
2b. "ISAPI_Rewrite 3" by HeliconTech (has free version too, that does the job pretty well)
You can accomplish what you are looking for without interacting with DNN at all by using an HttpModule. Kind of like this:
public class PidRewriteModule : System.Web.IHttpModule
{
public void Dispose()
{
}
public void Init(System.Web.HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app != null)
{
Match mPidCheck = new Regex(#"^/(?<pid>[0-9]+)/?$").Match(app.Context.Request.Url.AbsolutePath);
if (mPidCheck.Success)
{
app.Context.RewritePath("~/default.aspx", String.Empty, String.Concat("pid=", mPidCheck.Groups["pid"].Value));
}
}
else
return;
}
}
Then you can add this to your Web.config:
<modules runAllManagedModulesForAllRequests="true">
<add name="PidRewriteModule" type="Assembly.Namespace.PidRewriteModule, Assembly"/>
</modules>
Put that in the system.webServer node. Substitute Assembly and Namespace respectively.
All of this info is for IIS7. It's not entirely different for IIS 6, but previous implementations you have to go the route of ISAPI filters.

Remove HttpPage Extension in asp.net web application

I want to remove Http Page Extension like this
my actual page:
http://test.com/dashboard.aspx
i need to modify as follows
http://test.com/
for all redirection of .aspx page.
P.s:
i dont want to use URL rewriting.
Use asp.net 4's routing engine. You can specify a routing rule in asp.net 4 as a default route.
Check out:
http://www.xdevsoftware.com/blog/post/Default-Route-in-ASPNET-4-URL-Routing.aspx
for a very basic one that may work in your scenario try this in your global.asax.cs to map everything to say default.aspx
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Default", "{*whatever}", "~/default.aspx");
}
You can write HttpModule where you can scan incoming url and make all that you want before request will be processed.
http://msdn.microsoft.com/en-us/library/ms227673.aspx

MvcMiniProfiler results request giving 404 in Asp.Net MVC app

I am trying to use the MvcMiniProfiler in my Asp.Net MVC application. I installed the latest NuGet package and added all the code from the wiki page into Application_BeginRequest(), Application_AuthenticateRequest() and in my view.
Upon loading the page, all of the mini profiler's javascript files are being included and correctly downloaded from the server, but when it attempts to get the results Chrome shows:
GET http://localhost:59269/mini-profiler-results?id=59924d7f-98ba-40fe-9c4a-6a163f7c9b08&popup=1 404 (Not Found)
I assume this is due to no routes being setup with MVC to allow for the /mini-profiler-results however I cannot find a way to do that. Looking at the Google code page, their Global.asax.cs file of their sample app has gone through several changes, with one time using MvcMiniProfiler.MiniProfiler.RegisterRoutes(), a second using MvcMiniProfiler.MiniProfiler.Init(), and a third style which does nothing of the sort. The previously mentioned two functions do not exist, so I assume they have been phased out.
At this point I'm unsure of how I can fix this error and use the profiler in my app. Any ideas?
My Global.Asax.cs file looks like:
public class Global : System.Web.HttpApplication
{
protected void Application_BeginRequest()
{
MvcMiniProfiler.MiniProfiler.Start();
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
// Only show profiling to admins
if (!Roles.IsUserInRole(Constants.AdminRole))
MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
}
protected void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new { controller = "Home", action = "Index", id = "" }
// Parameter defaults
);
}
}
The RegisterRoutes() method is now called automatically (and with appropriate write-locks, etc) by the static constructor, which should in turn be called when you first call MiniProfiler.Start(). This should inject the routes into the route-table.
So unless you are explicitly clearing the route-table at some point after you have first touched the profiler, it should (all things being equal) work.
I wonder if this is a security thing. For example, which version of IIS are you running with? In some configs (IIS6 in particular) the path needs to be recognised by the server, or you need to enable a wildcard. If this is the case, please let me know - maybe we can implement some kind of fallback route to an ashx or something.
Update: the problem is that you aren't saving the results at the end of the query; there is both short-term and long-term storage, and both have default implementations provided - all you need to do is something like:
protected void Application_EndRequest(object sender, EventArgs e)
{
MiniProfiler.Stop(discardResults: !IsAnAdmin());
}
Update update: you may also need to add the following to web.config, in the <system.webServer>, <handlers> section:
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*"
type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified"
preCondition="integratedMode" />

inline to handle url redirect

I am migrating a site from siteA.domain.com to siteB.domain.com. All the page paths remain the same. The problem is it's a gradual migration, and not every path is being migrated at the same time. So what I need to do is check the path the user is going to, and if it's a member of a list of migrated sites, then redirect the user from siteA.domain.com/path to siteB.domain.com/path
I was hoping to add in-line c# code to the master page. Any thoughts/examples of this?
I believe the correct way would be to add some routes to the Global.asax.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("home",
"home",
"~/Home.aspx");
}
The above code will let you type "http://mysite.com/home" and it will redirect to the Home.aspx page. You could redirect to http://myothersite.com/Home.aspx instead of using the ~, or relative path.
You can add routes for each and every page that you have in some master list.
I would have a list of address in your config that have been migrated, then in the page_load of the master page check the current url (on of the properties in Request.Url I can't remember which) and see if it is the list from the config.
Simple, but quite often the simple way is the best. Plus if it is a temporary thing there is no point wasting time doing anything complex.
Any reason not to use IIS for the redirect? SO Question - How to redirect a URL path in IIS?
Create an IHttpHandler that intercepts all incoming requests and redirects appropriately.

Categories

Resources