Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Using "page" route parameter in convention routing fails with Asp.Net Core Mvc 2.0 #6660

Closed

Description

@eat-sleep-code

I have a website that was working perfectly fine in .NET core 1.1.

Today -- with the release of .NET Core 2.0 -- I decided to update my application. I changed my Target Framework to .NET Core 2.0, and switched over to using the Microsoft.AspNetCore.All package as outlined here: https://blogs.msdn.microsoft.com/webdev/2017/08/14/announcing-asp-net-core-2-0/

I installed the .NET Core 2.0 Runtime on my server. I restarted my server.

The application builds fine, but when I deploy, it seems my routes -- which worked perfectly in .NET Core 1.1 -- no longer work.

When I try using https://example.com/history (desired URL, which the route SHOULD direct to the "History" action of the "Pages" controller) it complains of a missing View.

If I use https://example.com/pages/history (not desired URL) it loads the home page, not the history page.

Again, this worked fine in .NET Core 1.1

This is my /Program.cs:

using Microsoft.AspNetCore.Hosting;
using System.IO;

namespace Framework
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

This is the portion of my /Startup.cs with the routes defined at the end:


            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "404",
                    template: "404/{id}",
                    defaults: new { controller = "Pages", action = "Error404"}
                );

                routes.MapRoute(
                    name: "news",
                    template: "news/{page}",
                    defaults: new { controller = "Pages", action = "News", page = 1 }
                );

                routes.MapRoute(
                    name: "history",
                    template: "history/{page=1}/{sortFilter?}/{sortOrder?}",
                    defaults: new { controller = "Pages", action = "History"}
                );

                routes.MapRoute(
                    name: "restoration-resources",
                    template: "restoration-resources/{page=1}/{sortFilter?}/{sortOrder?}",
                    defaults: new { controller = "Pages", action = "RestorationResources"}
                );

                routes.MapRoute(
                    name: "vehicles",
                    template: "vehicles/{vehicle?}/{page=1}/{sortFilter?}/{sortOrder?}",
                    defaults: new { controller = "Pages", action = "Vehicles" }
                );

                routes.MapRoute(
                    name: "gallery",
                    template: "gallery/{gallery?}",
                    defaults: new { controller = "Pages", action = "Gallery" }
                );

                routes.MapRoute(
                    name: "rss",
                    template: "rss/{source?}/{post?}",
                    defaults: new { controller = "Pages", action = "RSS"}
                );

                routes.MapRoute(
                    name: "pages",
                    template: "{action}/{id?}",
                    defaults: new { controller = "Pages", action = "News"}
                );

                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{id?}",
                    defaults: new { controller = "Pages", action = "News"}
                );
            });
        }
    }
}

Activity

  1. davidfowl commented on Aug 15, 2017

    @davidfowl
    Member

    Can you show the exception?

    PS: There are a bunch of things that can be cleaned up with your code in general but I'll discuss those after we figure out why your views can't be found.

  2. davidfowl commented on Aug 15, 2017

    @davidfowl
    Member

    One more thing, I'd suggest turning up the logging verbosity. That should show you exactly what's happening.

  3. eat-sleep-code commented on Aug 15, 2017

    @eat-sleep-code
    Author

    @davidfowl: Here ya go:

    System.InvalidOperationException: The view 'History' was not found. The following locations were searched:
    /Views/Shared/History.en-US.cshtml
    /Views/Shared/History.en.cshtml
    /Views/Shared/History.cshtml
    at Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations)
    at Microsoft.AspNetCore.Mvc.ViewResult.d__26.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__19.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__24.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__22.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__17.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__15.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()

  4. davidfowl commented on Aug 15, 2017

    @davidfowl
    Member

    @eat-sleep-code See my other suggesting about turning up the logging verbosity.

  5. eat-sleep-code commented on Aug 15, 2017

    @eat-sleep-code
    Author

    @davidfowl Can that be done through the config? If it requires a rebuild, I will have to wait until later as I am in the office now and the code for this site is at home.

  6. davidfowl commented on Aug 15, 2017

    @davidfowl
    Member

    @eat-sleep-code It can be done through configuration in 2.0.0 but you didn't update your code to use any of the new APIs.

    I'm assuming you also have views in the right folder? Can you show the layout on disk? Are you running behind IIS?

  7. eat-sleep-code commented on Aug 15, 2017

    @eat-sleep-code
    Author

    @davidfowl Do you have an outline of what code changes need to made to use the new APIs?

    Yes it is behind IIS. (Windows Server 2016 Datacenter)

    When I switched the DLL references in my project, I suddenly get a Framework.PrecompiledViews.dll file instead of the /Views folder.

    image

    This route, should (and used to) make it work.

    routes.MapRoute(
      name: "history",
      template: "history/{page=1}/{sortFilter?}/{sortOrder?}",
      defaults: new { controller = "Pages", action = "History"}
    );
    
  8. davidfowl commented on Aug 15, 2017

    @davidfowl
    Member

    @davidfowl Do you have an outline of what code changes need to made to use the new APIs?

    @eat-sleep-code https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/

    Just to be clear, I'm not saying this is the reason for any failures. It's more about enabling logging by default (and the ability to change it via configuration).

    When I switched the DLL references in my project, I suddenly get a Framework.PrecompiledViews.dll file instead of the /Views folder.

    Yes, this is one of the new defaults. Any clues if anything here needs to be changed @pranavkm ?

  9. eat-sleep-code commented on Aug 15, 2017

    @eat-sleep-code
    Author

    @davidfowl Thanks for that migration reference. I will have to tackle that stuff tonight.

    But you are right, nothing there seems to explain why the silly routes are not working.

  10. pranavkm commented on Aug 15, 2017

    @pranavkm
    Contributor

    @eat-sleep-code could you include a file list tree /f from your app root?

  11. pranavkm commented on Aug 15, 2017

    @pranavkm
    Contributor

    @eat-sleep-code - sorry, I was specifically looking for the apps Views directory which is missing here.

  12. eat-sleep-code commented on Aug 15, 2017

    @eat-sleep-code
    Author

    @pranavkm Yeah, doesn't get created. Instead I have the Framework.PrecompiledViews.dll.

  13. pranavkm commented on Aug 15, 2017

    @pranavkm
    Contributor

    I was looking for the views directory in your application root (not the output). Alternatively if you could share your application that would also work.

  14. eat-sleep-code commented on Aug 15, 2017

    @eat-sleep-code
    Author

    @pranavkm I will provide a repo tonight when I get home.

  15. self-assigned this
    on Aug 15, 2017
  16. 28 remaining items

  17. M-S-M-A-S commented on Aug 29, 2017

    @M-S-M-A-S

    I have the same bug but your solution doesn't fix it

    routes.MapRoute(
                        name: null,
                        template: "Community/Page{pageID}",
                        defaults: new { Controller = "Forum", action = "Home" });
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Forum}/{action=Home}/{id?}");
                    routes.MapRoute(
                        name: null,
                        template: "",
                        defaults: new { controller = "Forum", action = "Home", pageID = 1 });
                    routes.MapRoute(name: null, template: "{controller}/{action}/{id?}");
    

    this issue happen because i'm using tag helper

        [HtmlAttributeName(DictionaryAttributePrefix = "page-url-")]
          public Dictionary<string, object> pageUrlValues { set; get; }
          public override void Process(TagHelperContext context, TagHelperOutput output)
          {
            pageUrlValues["**page**"] = i;
    

    if i changed page to pageid the url Community/Page3 display page1
    and if i used it as page, now when i navigate between pages they update but i get page1?page=3
    note that since it show ?page=3 now my mapping is useless

  18. changed the title [-].NET Core 2.0 route configuration being ignored[/-] [+]Using "page" route parameter in convention routing fails with Asp.Net Core Mvc 2.0[/+] on Sep 11, 2017
  19. added 2 commits that reference this issue on Sep 18, 2017
    9250a65
    9f5e4eb
  20. added a commit that references this issue on Oct 4, 2017
  21. BingZL1983 commented on Nov 13, 2017

    @BingZL1983

    Hi I have the same issue with routing. The following code worked, but the result looks like this: http://localhost:51409/?page=2, which is not ideal
    routes.MapRoute(
    name: null,
    template: "",
    defaults: new { controller = "Product", action = "List" }
    );

    but when I add {page} variable into Template as following: then the view List is not found.
    routes.MapRoute(
    name: null,
    template: "Page{page:int}",
    defaults: new { controller = "Product", action = "List" }
    );

    Can anyone help me out? Thank you!

  22. davidfowl commented on Nov 13, 2017

    @davidfowl
    Member

    Change route name “page” to something else

  23. BingZL1983 commented on Nov 14, 2017

    @BingZL1983

    Hi Davidfowl

    Thank you for your response. I have change variable "page" to "pg" as following.

    public ViewResult List(string category, int pg = 1)
    => View(new ProductsListViewModel{
    Products = repository.Products
    .Where(p => category == null || p.Category == category)
    .OrderBy(p => p.ProductID)
    .Skip((pg - 1) * PageSize)
    .Take(PageSize),
    PagingInfo = new PagingInfo
    {
    CurrentPage = pg,
    ItemsPerPage = PageSize,
    TotalItems = category == null ?
    repository.Products.Count() :
    repository.Products.Where(e => e.Category == category).Count()
    },
    CurrentCategory = category
    });

    routes.MapRoute(
    name: null,
    template: "{pg}",
    defaults: new { controller = "Product", action = "List" }
    );

    it still didn't work. Do you think it is a bug in ASP Net Core 2.0?

    FYI, I started a web app project using empty template. Thank you!

  24. Temoxa commented on Nov 30, 2017

    @Temoxa

    Hi, I have the same problem BIngZL1983...((

    use ASP Net Core 2.0,

    but i resolved problem change parametr "page" in routes.... =)

  25. pranavkm commented on Nov 30, 2017

    @pranavkm
    Contributor

    @Temoxa, the issue was fixed as part of the 2.0.3 release. Upgrading your package reference - Microsoft.AspNetCore.All to 2.0.3 in CoreCLR projects or Microsoft.AspNetCore.Mvc to 2.0.1 in desktop projects should address this without requiring code changes.

  26. Temoxa commented on Nov 30, 2017

    @Temoxa

    Today upgrade to 2.0.3 , but problem saves....(

  27. serpent5 commented on Dec 16, 2017

    @serpent5

    I see a similar issue, but related to using page as a query-string parameter inside of a Razor Pages model. e.g.:

    public void OnGet(string page)
    

    Using this, page is set to the path for the current page, which is unexpected. I'm seeing this when using Microsoft.AspNetCore.All 2.0.3 too.

    The problem first showed up when using int page, which always comes out as 0 due to the difference in data type when binding.

    As suggested, changing this to something like pageNumber is a solution, but that's going to be a pain when using a convention of e.g. int page, int pageSize for pagination.

  28. pranavkm commented on Dec 18, 2017

    @pranavkm
    Contributor

    @Temoxa could you file a new issue that details the issue you're running in to?
    @serpent5 the term page like the terms controller, action or area is reserved by Mvc for routing and it's attempting to bind the route value. Unfortunately, there isn't a very good solution to this outside of using a different name.

  29. Temoxa commented on Dec 19, 2017

    @Temoxa

    My problem is resolved in version 2.0.3

  30. added a commit that references this issue on Jan 24, 2026
    7b2a4ff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions