2

I have the following route

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

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

So the following URLs are routed

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

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

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

In my controller, I have a conditional check to see if the passed URL exists in my lookup table. For example

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

The routing is working in terms of the en/hotels path, if the incorrect URL is entered like below, the actual URL is just returned like normal.

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

Basically, I am wanting to build up a dictionary of URLs that I want to route, and if the requested URL doesn't exist in the dictionary I want to returned the requested URL whether that be a .HTM, .HTML or .ASP page.

neildt
  • 5,101
  • 10
  • 56
  • 107
  • May I know are you still looking for the answer ? – Shaiju T May 14 '19 at 12:17
  • Is it that you're wanting to redirect all your 'attractions' URLs to a specific page? Also, I'm not sure how the 'attractions' URL are related to your 'hotels' URLs and Route. – cyrotello May 14 '19 at 12:32
  • Yes I'm still looking for a answer @stom – neildt May 16 '19 at 13:57
  • If this code is reached the requested url has to be like **/en/hotels/** so I don't understand where you could redirect to, the url can never be something like **/en/attractions/** at this point. – colinD May 18 '19 at 17:28

3 Answers3

1

I do not fully understand your question, but if you want to serve static html files apart from the routes you have defined, you can have a public or static folder and allow access to html files.

You can do this by creating a Web.config file inside this folder and register the handlers:

<?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>

Then, any file .htm or .html file will be served when requested.

Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53
0

It seems like you're wanting to creating a "catch-all" route at the end of the route table:

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

Check out this answer: https://stackoverflow.com/a/310636/879352

In this way, you can build the dictionary of URLs you want to route (above the catch-all route), and whatever isn't caught in the dictionary will directed to the 404 page.

cyrotello
  • 757
  • 9
  • 26
0

Seems like a better way to handle these requests is by adding a custom Middleware to the pipeline.
Basically your request goes through multiple filters (like the static files middleware) until it finally reaches the routing middleware.
This is the only way I am aware of to maintain information about the request internally (not with GET parameters) after it goes through the routing middleware.

Example:

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();
     }
});
mitiko
  • 751
  • 1
  • 8
  • 20