问题
from the following url :
http://www.mywebsite.com/default.aspx
I want to remove the default.aspx so the url will look like :
http://www.mywebsite.com/
I need a quick and clean way to do this , and I only need to do this with default.aspx page and no ther pages . thanks in advance .
回答1:
change your webconfig with below code:it solve my same problem.
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="default.aspx Redirect" stopProcessing="true">
<match url="^(.*\/)*default\.aspx$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
回答2:
you can use Routing
in Global.asax file
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("defualt",
"",
"~/Default.aspx");
}
and in your redirection page call
Response.Redirect(GetRouteUrl("defualt", null));
for more info read : http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx
回答3:
By default your default page can vary it can be default.html or index.html or default.aspx depending on the preference. So if you just want to remove it from URL you have mentioned dont specify page name and default.aspx opens by default and wont be visible in url as well
Whereas if you want to do URL rewriting,there are various ways you can do this, you can make changes in web.config or can have a httpModule of your own have a look at below link:-
http://www.codeproject.com/Articles/2538/URL-Rewriting-with-ASP-NET
回答4:
You can use the Uri class
var uri = new Uri("http://www.mywebsite.com/default.aspx");
string urlIWant = uri.GetLeftPart(UriPartial.Authority);
来源:https://stackoverflow.com/questions/17399220/how-to-remove-page-name-from-url-in-asp-net