页面未正确重定向、URL 重写 (Asp.NET)

发布于 2024-10-25 19:48:09 字数 3070 浏览 1 评论 0原文

我正在研究 URL 重写,我在 asp.net 网站上找到了一个教程, 我这样做的方式是

输入 URL http://localhost:1573/WebNew/web /first-web

现在我已经写了一个类

public class FixURLs : IHttpModule 
{
    public FixURLs()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region IHttpModule Members

public void Dispose()
{
    // do nothing
}

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(context_BeginRequest);
}

#endregion

void context_BeginRequest(object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;

    /*// checking page extension
    switch (System.IO.Path.GetExtension(app.Request.Url.AbsoluteUri.ToLower()))
    {
        case ".bmp":
        case ".gif":
        case ".jpg":
        case ".jpe":
        case ".jpeg":
        case ".png":
        case ".css":
        case ".js":
        case ".txt":
        case ".swf":
            // don't redirect, these requests may required in many cases
            return;
            break;
    }*/

    if (app.Request.RawUrl.ToLower().Contains("/web/"))
    {
        **if (app.Request.RawUrl.ToLower().Contains(".png")
              || app.Request.RawUrl.ToLower().Contains(".gif")
              || app.Request.RawUrl.ToLower().Contains(".js"))
        {
            return;
        } **
        DatabaseLayer dbLayer = new DatabaseLayer();

        string urlFromBrowser = app.Request.RawUrl.ToLower();
        string[] urlFormat = urlFromBrowser.Split('/');
        urlFromBrowser = urlFormat.GetValue(2).ToString() + "/" + urlFormat.GetValue(3).ToString();

        int WebId = dbLayer.GetWebURLId(urlFromBrowser.Trim());

        app.Context.RewritePath("Default.aspx", "", "WebId="+WebId);

    }
}

,但问题是,它没有重定向到 Default.aspx 页面。

我收到以下错误:

描述:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下 URL 并确保拼写正确。

请求的 URL:/WebNew/web/Default.aspx

我可以看到它正在请求 URL /webNew/web/Default.aspx 但我只需要 /webnew/default.aspx?WebId=2

请帮助我

编辑< /strong>

感谢您的回答,但我无法接受您的回答,评论按钮也对我不起作用,这就是我编辑帖子的原因。我收到 JavaScript 错误:需要对象,而对象问题为空。 在此处输入图像描述

@Waqas Raja 感谢您的回答,现在工作正常。但是当我将断点放置在 Default.aspx 页面的 (!IspostBack) 事件处时,我可以看到它出现了大约 8 次。加载页面后,一些图像消失了。多次刷新是问题吗?知道为什么它会一次又一次地发生 if(!IsPostBack) 事件吗? 感谢您的帮助

EDIT1

@Waqas Raja 我会在从不同的浏览器登录后接受您的回答。

EDIT2

@Waqas Raja 我在我的主页上显示图像,如下

所示 还有其他一些图像也以这种方式显示,它们在正常加载时都正常,但当我使用 URL 重写时我只能看到替代文本。

我也尝试过这个

但结果相同。 :-( 有什么建议吗?

通过检查原始网址,我可以看到除了页面之外还有很多请求,即 jpg、.js,就像您提到的那样。 我已经更新了代码,是否正确?因为我看不到图像和一些 JavaScript 函数。

EDIT3

@Waqas Raja 非常感谢您的帮助。我现在可以看到图像了。我已将 HTML 图像删除到 asp:Image controla 并将 ImageURL 保留为 ~/images/facebook_icon.png。

但是页面上还是看不到JS文件效果,我把src="~/JS/jQuery.js"但是还是不行。我已将您提到的代码放在我的班级中[就在您指导我的地方]。

I am working on URL rewrite and I found one tutorial on asp.net site,
The way I am doing it is

URL I am entering http://localhost:1573/WebNew/web/first-web

Now I have wriiten one class

public class FixURLs : IHttpModule 
{
    public FixURLs()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region IHttpModule Members

public void Dispose()
{
    // do nothing
}

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(context_BeginRequest);
}

#endregion

void context_BeginRequest(object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;

    /*// checking page extension
    switch (System.IO.Path.GetExtension(app.Request.Url.AbsoluteUri.ToLower()))
    {
        case ".bmp":
        case ".gif":
        case ".jpg":
        case ".jpe":
        case ".jpeg":
        case ".png":
        case ".css":
        case ".js":
        case ".txt":
        case ".swf":
            // don't redirect, these requests may required in many cases
            return;
            break;
    }*/

    if (app.Request.RawUrl.ToLower().Contains("/web/"))
    {
        **if (app.Request.RawUrl.ToLower().Contains(".png")
              || app.Request.RawUrl.ToLower().Contains(".gif")
              || app.Request.RawUrl.ToLower().Contains(".js"))
        {
            return;
        } **
        DatabaseLayer dbLayer = new DatabaseLayer();

        string urlFromBrowser = app.Request.RawUrl.ToLower();
        string[] urlFormat = urlFromBrowser.Split('/');
        urlFromBrowser = urlFormat.GetValue(2).ToString() + "/" + urlFormat.GetValue(3).ToString();

        int WebId = dbLayer.GetWebURLId(urlFromBrowser.Trim());

        app.Context.RewritePath("Default.aspx", "", "WebId="+WebId);

    }
}

But the problem is that, it is not redirecting to the Default.aspx page.

I am getting the below error:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /WebNew/web/Default.aspx

I can see that it is requesting the URL /webNew/web/Default.aspx but I just need /webnew/default.aspx?WebId=2

Please assist me

EDIT

Thnks for the answer, but I am not able to accept your answer nor the comment button is working for me, that is the reason I am editing my post . I am getting JavaScript error: Object expected and Object question is null.
enter image description here

@Waqas Raja Thanks for you answer, it is working fine now. But when I placed the breakpoint at (!IspostBack) event of Default.aspx page, I can see that it is coming aroung 8 times. After loading the page few images are gone. Is multiple refresh is the issue? Any idea why it is going again and again to if(!IsPostBack) event.
Thanks for your help

EDIT1

@Waqas Raja I will accept you answer after login from the differnt browser.

EDIT2

@Waqas Raja I am showing image on my home page like this

There are other few images which are also being displayed in this manner, they all are coming properly when loading normally but when I am using URL rewriting I can see only alt text.

I have tried this also

but same result. :-( any suggestion?

By checking the raw url i can see that there are many request other than the page, i.e. jpg,.js just like you mentioned.
I have updated the code, is it correct? because I am not able to see images and few javascript functions.

EDIT3

@Waqas Raja Thanks a lot for your assistance. I can now see the images. I have removed the HTML image to asp:Image controla and keeping ImageURL as ~/images/facebook_icon.png.

But I still can't see the JS file effects on the page, I put src="~/JS/jQuery.js" but it is still not working. I have placed the code mentioned by you in my Class [Just where you have guided me].

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2024-11-01 19:48:09

问题出在您的重写路径上,请将其替换为以下内容

app.Context.RewritePath("~/webnew/Default.aspx", "", "WebId=" + WebId);

编辑 1 您的要求已更改,但是让我尝试解释一下出了什么问题。

问题一定出在图像的相对路径上,并且在外部 javascript 和 css 的情况下也可能发生。

您应该使用相对于根目录的路径。

~ 指的是正在访问其资源(通常是页面或有时是外部CSS)的虚拟目录的根目录。

~/pathToImagesFolder/imageName.ext

在包含 javascript 和 css 时应谨慎,并使用相对于网站主目录或虚拟目录的路径。然而在 css 内部不需要采取这方面的步骤。

关于 IsPostBack 问题,

我猜问题出在您的 if 条件

if (app.Request.RawUrl.ToLower().Contains("/web/"))

这里,您正在重定向 URL 中包含 /web/ 的任何请求,并且很可能在您的页面内您正在访问一些资源例如图像或外部 css 或 js,其 url 中包含 /web/ ,因此当请求到达时,它会被重定向到 default.aspx 并引发问题。您可以通过查看 Default.aspxPage_Load 事件中的 Request.RawUrl 来确认这一点。

因此,在比较请求的资源路径和重定向。如果您检查并跳过重定向所有以 .css .jpg .JPG .js .gif 等扩展名结尾的网址,那就更漂亮了,

我希望您现在能够理解。

编辑2您需要跳过所有其他扩展

// checking page extension
switch (System.IO.Path.GetExtension(app.Request.Url.AbsoluteUri.ToLower()))
{
    case ".bmp":
    case ".gif":
    case ".jpg":
    case ".jpe":
    case ".jpeg":
    case ".png":
    case ".css":
    case ".js":
    case ".txt":
    case ".swf":
        // don't redirect, these requests may required in many cases
        return;
        break;
}

The problem is with your rewrite path, replace it with the following

app.Context.RewritePath("~/webnew/Default.aspx", "", "WebId=" + WebId);

Edit 1 Your requirements have been changed, however let me try to explain whats going wrong.

The problem must be with your relative paths of images and also it could occur in case of external javascript and css.

You should use path relative to your root directory.

~ refers to Root directory of virtual directory whose resource (usually page or sometime external css) is being accessed.

~/pathToImagesFolder/imageName.ext

You should be cautious when including javascript and css and use path relative to home directory of your website or virtual directory. However inside css there is no step needed to be taken in this regard.

Regarding IsPostBack problem

I guess the issue is with your if condition

if (app.Request.RawUrl.ToLower().Contains("/web/"))

Here you are redirecting any request which contains /web/ in URL and most probably inside your page you are accessing some resources like images or external css or js which contains /web/ in their url and so when the request reaches it is being redirected to default.aspx and the issue raises. You can confirm it by looking into Request.RawUrl in Page_Load event of Default.aspx

So, you need more checks when comparing requested resource path and redirecting. More beautiful it be if you check and skip redirecting all urls which ends with the extensions like .css .jpg .JPG .js .gif e.t.c

I hope your understandings build now.

Edit 2 You need to skip all the other extensions

// checking page extension
switch (System.IO.Path.GetExtension(app.Request.Url.AbsoluteUri.ToLower()))
{
    case ".bmp":
    case ".gif":
    case ".jpg":
    case ".jpe":
    case ".jpeg":
    case ".png":
    case ".css":
    case ".js":
    case ".txt":
    case ".swf":
        // don't redirect, these requests may required in many cases
        return;
        break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文