自定义404页面不会被重定向 [英] Custom 404 Page Doesn't Get Redirected

查看:84
本文介绍了自定义404页面不会被重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户在浏览器中输入错误的url时,我都试图重定向到404自定义错误页面.假设用户输入 https://localhost:44332/foo (foo页面没有(不存在),则应将其重定向到自定义错误页面.就我而言,不是,但是,它可以正常工作:

I am trying to redirect to 404 custom error page whenever a user types wrong url in the browser. Say a user types https://localhost:44332/foo (foo page doesn't exist), then it should redirect to the custom error page. In my case, it doesn't, but for this, it works:

https://localhost:44332/foo/foo-page

此重定向到自定义错误页面.我在项目中做了一些小练习,并相信,这会导致此重定向问题- https://localhost:44332/foo .

This get redirected to custom error page. I did a small workout in my project and believe, this causes an issue for this redirection - https://localhost:44332/foo.

我有一个页面,说 user-details ,它使用查询字符串重定向,如下所示:

I've a page, say user-details which redirects with a query string something like this:

https://localhost:44332/user-details?=1002

因此,在 Startup.cs 文件中,我这样做是为了使url用户友好:

So in the Startup.cs file, I did this to make the url user-friendly:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddRazorPagesOptions(options =>
    {
        //Here is the routing done
        options.Conventions.AddPageRoute("/user-details", "{id}");
    });

    services.AddEntityFrameworkSqlite().AddDbContext<MyDbContext>();
}

因此,当用户键入 https://localhost:4433/1002 时,上述代码的作用是什么 ,它会将相关的详细信息重定向到用户详细信息页面(注意:我从正常工作的网址中省略了用户详细信息页面名称).因此,现在每当我输入不存在的错误页面名称时,都说foo或其他内容( https://localhost:44332/foo ),而不是重定向到错误页面,而是重定向到 user-details 页面.我错过了或需要做使其相应工作的任何内容吗?

So what the above code does, when a user types https://localhost:4433/1002, it gets the related details redirecting to user details page (Note: I omitted the user-details page name from the url that works fine). So now whenever I type a wrong page name that doesn't exist, say foo or whatever (https://localhost:44332/foo), instead of redirecting to error page, gets redirected to the user-details page. Anything that I missed or required to do to make it work accordingly?

NB :按以下方式处理 Startup.cs 文件中的错误

N.B: Handling errors in the Startup.cs file as follows

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   if (env.IsDevelopment())
   {
      app.UseDeveloperExceptionPage();
   }
   else
   {
      //Error handling here
      app.UseStatusCodePages();
      app.UseExceptionHandler("/Error");

      //The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
      app.UseHsts();
    }

  app.UseMvc();
} 

推荐答案

问题是:

options.Conventions.AddPageRoute("/user-details", "{id}");

仅仅因为它被命名为 id 并不意味着它将以某种方式仅采用整数.您实际上已经创建了一条路线,该路线将捕获任何内容而没有其他路由段:即/foo 将捕获,而/foo/bar 将不会捕获.您可以添加一个约束来进行帮助,即"{id:int}" ,然后该约束仅在路由段可以转换为int时才能捕获.

Just because it's named id doesn't mean that it's going to somehow only take ints. You've essentially created a route that will catch anything without an additional route segment: i.e. /foo will catch, but /foo/bar won't. You can add a constraint to help, i.e "{id:int}", which will then only catch if the route segment can be converted to an int.

这篇关于自定义404页面不会被重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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