我正在asp.net中创建页面。我创建了名为WebForm的Form.aspx。现在,我希望所有请求*.htm请求都能加载这个页面,在那里我需要做我需要做的事情(也许不是最好的方法,但它可以根据我的需要工作)。所以我创造了这样的东西:
routes.MapPageRoute(null, "{file}.htm", "~/Pages/Form.aspx");
routes.MapPageRoute(null, "{folder}/{file}.htm", "~/Pages/Form.aspx");
现在,像http://example.com/whatever.htm或http://example.com/whatever/whatever.htm这样的东西都被重定向到了我的Form.aspx上。但是这个Form.aspx本身没有任何意义。因此,下面的页面是无稽之谈,http://example.com/Pages/Form.aspx。我怎么才能使它无效?因此,它将向我展示类似于"Error 404.0 - Not“之类的内容。我想要同样的行为,就像我会写"http://example.com/doesntexist.aspx“一样。我不想做任何重定向(只有当没有其他选项存在)。到目前为止,我只尝试过这样的方法(这是行不通的):
routes.MapPageRoute(null, "Pages/Form.aspx", "~/doesntexist.aspx");
它什么也做不了..。任何帮助都很感激。
发布于 2014-10-29 03:15:28
在Global.asax中添加以下代码:
..。
protected void Application_BeginRequest(object sender, EventArgs e)
{
string requestPath = Request.RawUrl.Trim().ToLower();
HttpApplication app = sender as HttpApplication;
if (!IsLocalRequest(app.Context.Request))
{
if (requestPath.IndexOf("form.aspx") > 0)
{
throw new HttpException(404, "Error HTTP 404.0 – Not Found.");
}
}
}
// This method determines whether request came from the same IP address as the server.
public static bool IsLocalRequest(HttpRequest request)
{
return request.ServerVariables["LOCAL_ADDR"] == request.ServerVariables["REMOTE_ADDR"];
}
..。
https://stackoverflow.com/questions/26618355
复制相似问题