首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使表单页无效?

如何使表单页无效?
EN

Stack Overflow用户
提问于 2014-10-28 20:58:37
回答 1查看 130关注 0票数 0

我正在asp.net中创建页面。我创建了名为WebForm的Form.aspx。现在,我希望所有请求*.htm请求都能加载这个页面,在那里我需要做我需要做的事情(也许不是最好的方法,但它可以根据我的需要工作)。所以我创造了这样的东西:

代码语言:javascript
运行
复制
routes.MapPageRoute(null, "{file}.htm", "~/Pages/Form.aspx");
routes.MapPageRoute(null, "{folder}/{file}.htm", "~/Pages/Form.aspx");

现在,像http://example.com/whatever.htmhttp://example.com/whatever/whatever.htm这样的东西都被重定向到了我的Form.aspx上。但是这个Form.aspx本身没有任何意义。因此,下面的页面是无稽之谈,http://example.com/Pages/Form.aspx。我怎么才能使它无效?因此,它将向我展示类似于"Error 404.0 - Not“之类的内容。我想要同样的行为,就像我会写"http://example.com/doesntexist.aspx“一样。我不想做任何重定向(只有当没有其他选项存在)。到目前为止,我只尝试过这样的方法(这是行不通的):

代码语言:javascript
运行
复制
routes.MapPageRoute(null, "Pages/Form.aspx", "~/doesntexist.aspx");

它什么也做不了..。任何帮助都很感激。

EN

Stack Overflow用户

回答已采纳

发布于 2014-10-29 03:15:28

在Global.asax中添加以下代码:

..。

代码语言:javascript
运行
复制
    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"];
}

..。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26618355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档