【问题标题】:How to route specific URL in MVC, otherwise return requested URL如何在 MVC 中路由特定的 URL,否则返回请求的 URL
【发布时间】:2019-09-28 19:06:01
【问题描述】:

我有以下路线

    public static void RegisterRoutes(RouteCollection routes)
    {            
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("Hotel", "en/hotels/{*hotelName}",
            new { controller = "Hotel", action = "index" });
    }

所以下面的URL被路由

www.mydomain.com/en/hotels/my-hotel-name/

www.mydomain.com/en/hotels/myhotelname/

www.mydomain.com/en/hotels/my_hotel_name/

在我的控制器中,我有一个条件检查以查看传递的 URL 是否存在于我的查找表中。例如

public class HotelController : Controller
{
    [HttpGet]
    [ActionName("Index")]
    public ActionResult IndexGet(string hotelFolder)
    {
        if(CheckIfFolderExists(hotelFolder))
        {
             return Content("Hotel URL Found");
        }      
        else
        {
            **//Here, I want to return the actual requested URL as it didn't exist in my lookup table
        }
    }
}

路由是根据 en/hotels 路径工作的,如果像下面这样输入了错误的 URL,则正常返回实际的 URL。

www.mydomain.com/en/attractions/my-attraction-name/

基本上,我想建立一个我想要路由的 URL 字典,如果字典中不存在请求的 URL,我想返回请求的 URL,无论是 .HTM、.HTML 还是.ASP 页面。

【问题讨论】:

  • 请问您还在寻找答案吗?
  • 您是否希望将所有“景点”网址重定向到特定页面?另外,我不确定“景点”网址与您的“酒店”网址和路线有何关系。
  • 是的,我仍在寻找答案@stom
  • 如果到达此代码,请求的 url 必须像 /en/hotels/ 所以我不明白你可以重定向到哪里,url 永远不会是什么就像 /en/attractions/ 在这一点上。

标签: c# .net asp.net-mvc routing routes


【解决方案1】:

我不完全理解你的问题,但是如果你想在你定义的路由之外提供静态 html 文件,你可以有一个 publicstatic 文件夹并允许访问 html 文件。

您可以通过在此文件夹中创建一个 Web.config 文件并注册处理程序来做到这一点:

<?xml version="1.0"?>

<configuration>
  <system.webServer>
    <handlers>
      <add name="HtmScriptHandler" path="*.htm" verb="GET"
          preCondition="integratedMode" type="System.Web.StaticFileHandler" />
      <add name="HtmlScriptHandler" path="*.html" verb="GET"
         preCondition="integratedMode" type="System.Web.StaticFileHandler" />
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

然后,任何文件 .htm.html 文件将在请求时提供。

【讨论】:

  • 我已添加到我的问题中以获得更多说明。
【解决方案2】:

您似乎想在路由表的末尾创建一个“包罗万象”的路由:

public static void RegisterRoutes(RouteCollection routes)
{            
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute("Hotel", "en/hotels/{hotelName}",
        new { controller = "Hotel", action = "index" });

    // Catch-all Route:
    routes.MapRoute("404-PageNotFound", "{*url}",
        new { controller = "YourController", action = "PageNotFound" } );
}

看看这个答案:https://stackoverflow.com/a/310636/879352

通过这种方式,您可以构建要路由的 URL 字典(在 catch-all 路由之上),字典中未捕获的任何内容都将定向到 404 页面。

【讨论】:

  • 我已添加到我的问题中以获得更多说明。
【解决方案3】:

似乎处理这些请求的更好方法是向管道添加自定义Middleware
基本上,您的请求会经过多个过滤器(如静态文件中间件),直到最终到达路由中间件。
这是我知道在通过路由中间件后在内部维护有关请求的信息(而不是使用 GET 参数)的唯一方法。

例子:

app.Use(async (context, next) =>
{
     var path = context.Request.Path;
     // Get the name of the hotel
     if(CheckIfFolderExists(path.Substring("/en/hotels/".Length))
     {
          await context.Response.WriteAsync("URL FOUND");
          // or write a file to the response
     }      
     else
     {
          // else let the request through the pipeline
          await next.Invoke();
     }
});

【讨论】:

  • 这是 ASP.NET Core 顺便说一句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 2022-07-28
  • 2019-01-13
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多