在C#中,我怎样才能使它像目录一样结束.aspx?像file.aspx / programtorun /

时间:2013-08-14 21:01:54

标签: c# asp.net

所以我一直在使用php,并且因为我喜欢它而切换到ASP.NET。无论如何......我一直在开发自己的网站等等,但我无法想象我的生活!

我有什么:

string val = HttpContext.Current.Request["Header"];
// filename: index.aspx
// what I need to get
if (val == "random")
{
    renderA.Random("randoms.html");
}
else
{
    renderA.Index("index.html");
}

它在URI中返回的内容

/index.asp?random

我希望它看起来像:

/index.aspx/random/

有没有我可以解决这个问题?

3 个答案:

答案 0 :(得分:2)

你应该看看切换到MVC网站;它将为您提供您正在寻找的功能。

这是旧版本,但您会明白:Scott Guthrie talking about mvc framework

答案 1 :(得分:1)

我假设您使用的是WebForms而不是MVC。它被称为FriendlyUrls,这是Hanselman的教程。您可能需要考虑切换到MVC,因为它可以使用Routing执行此操作。

答案 2 :(得分:1)

向项目中添加Global Application Class(Global.asax文件),然后您可以执行以下操作:

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    RegisterRoutes(RouteTable.Routes);

}


void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("random", "random", "~/index.html");
}

您需要引用System.Web.Routing

<%@ Import Namespace="System.Web.Routing" %>

了解详情:

http://msdn.microsoft.com/en-us/library/cc668201.ASPX

http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET-4-0