页面未正确重定向,URL重写(Asp.NET) [英] Page not redirecting properly, URL rewriting (Asp.NET)

查看:236
本文介绍了页面未正确重定向,URL重写(Asp.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的URL重写,我发现在asp.net站点一个教程,
我这样做的方式是

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

URL我进入的http://本地主机:1573 / WebNew / WEB /第一的Web

现在我已经wriiten一个班

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);

    }
}

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

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

我收到以下错误:

说明:HTTP 404。您正在寻找(或它的一个依赖)可能已被删除的资源,更名或暂时不可用。请检查以下URL并确保其拼写正确。

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.

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

Requested URL: /WebNew/web/Default.aspx

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

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

请帮助我

修改

的答案Thnks,但我不能接受你的答案,也没有评论按钮为我工作,这是我编辑我的职位的原因。我得到的JavaScript错误:预期的对象和对象的问题为空。

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.

@Waqas拉贾感谢您的回答,现在是工作的罚款。但是,当我把断点(!的IsPostBack)Default.aspx页面的情况下,我可以看到它是未来aroung 8 倍。在页面加载后几张图片都没有了。在多个刷新的问题?任何想法,为什么它是一次又一次去,如果(!的IsPostBack)事件。
感谢您的帮助。

@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拉贾我会接受你从不同的充浏览器登录后回答。

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

EDIT2

@Waqas拉贾我显示的

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

有哪些也正在以这种方式显示其他一些图片,他们都正常加载,但是当我使用URL重写我只能看到alt文字来时正确。

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.

我也试过这种


但同样的结果。 :-(任何建议?

but same result. :-( any suggestion?

通过检查的原始URL,我可以看到有比页面等诸多要求,即JPG,.js文件,就像你提到的。
我已经更新了code,它是正确的?因为我无法看到的图像和一些JavaScript函数。

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拉贾非常感谢你的帮助。我现在可以看到的图像。我已删除HTML图像到ASP:图像controla并保持IMAGEURL为〜/图片/ facebook_icon.png

@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.

但我仍然无法看到页面上的JS文件的影响,我把SRC =〜/ JS /的jquery.js,但它仍然没有工作。我已经把你提到的code在我的课[究竟出在哪里你指导我。

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].

推荐答案

但问题是你重写路径,用下面的替换

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

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

修改1 您的要求已被更改,但是让我尝试解释一下怎么回事错了。

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

问题必须与图像的相对路径,也可能发生在外部JavaScript和CSS的情况下。

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.

指根虚拟目录的目录,它的资源(通常是页面或某个外部CSS)被访问。

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

〜/ pathToImagesFolder / imageName.ext

,包括JavaScript和CSS时,您应该谨慎,并使用相对路径到您的网站或虚拟目录的主目录。但是内部的CSS没有必要在这方面要采取步骤

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.

关于的IsPostBack 问题

我想这个问题是你的,如果条件

I guess the issue is with your if condition

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

在这里,您重定向包含 / WEB /在网址并极有可能你的页面中,你正在访问喜欢其中包含图像或外部CSS或js的一些资源<任何请求code> / WEB / 在他们的网址,所以当请求到达它被重定向到的Default.aspx和问题提出。你可以寻找到 Request.RawUrl 的Page_Load 的Default.aspx的<事件确认/ code>

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

所以,你比较请求的资源路径和重定向时需要更多的检查。更漂亮,如果你检查并跳过重定向其与像的.css .JPG .JPG的.js .gif注意: e.t.c

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.

编辑2 您需要跳过所有其他分机

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;
}

这篇关于页面未正确重定向,URL重写(Asp.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆