E
E
ErichZann2018-02-25 15:50:55
ASP.NET
ErichZann, 2018-02-25 15:50:55

Call only 1 filter out of several for a specific Razor Pages Route?

There are 2 domains (.com and .ru) and 2 URLs like mysite.com/about-us and mysite.ru/o-nas which should return the same AboutUs.cshtml (Razor Page). Also, the defined URL must match its domain: /about-us for .com and mysite.ru/o-nas for .ru . For example:
site.COM/o-nas should not work and return Not Found(404)
site.RU/about-us should not work and return Not Found(404)
The site as indicated in the question is made by Razor Pages. I found a solution with Filters and it works, but both filters work for both site.com/about-us and site.ru/o-nas. Is it possible to somehow make only 1 filter work for each of the 2 URLs described above. Is it possible?
Thanks in advance, my code is below:

public static class DomainFilters
    {
         public static IPageApplicationModelConvention DomainEng(this PageConventionCollection con, string pageName, string route="")
        {
            return con.AddPageApplicationModelConvention(pageName, model =>
            {
                model.Filters.Add(new EnglishActionFilter(route));
            });
        }
        public static IPageApplicationModelConvention DomainRussian(this PageConventionCollection con, string pageName, string route = "")
        {
             return con.AddPageApplicationModelConvention(pageName, model =>
            {
                model.Filters.Add(new RussianActionFilter(route));
            });
        }
    }


    public class EnglishActionFilter : IResultFilter
    {
        public void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.HttpContext.Request.Host.ToString().Contains(".ru"))
            {
                context.Result = new NotFoundResult();
            }
        }

        public void OnResultExecuted(ResultExecutedContext context) { }
    }

    public class RussianActionFilter : IResultFilter
    {
        public void OnResultExecuting(ResultExecutingContext context)
        {
            if (context.HttpContext.Request.Host.ToString().Contains(".com"))
            {
               context.Result = new NotFoundResult();
            }
        }

        public void OnResultExecuted(ResultExecutedContext context){}
    }

Also code for ConfigureServices from Startup.cs:
public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .AddRazorPagesOptions(options =>
                    {
                        options.Conventions.DomainEng("/AboutUs", "about-us");
                        options.Conventions.DomainRussian("/AboutUs", "o-nas");
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question