locked
Core 2.2 razor pages and IIS RRS feed

  • Question

  • User55524441 posted

    Hi,

    I am working on upgrading existing Razor pages (v4.0.30319) application to asp net core 2.2 razor pages application. The application was not using routing and instead navigation to different pages was getting done using IIS rules (inbound and outbound) but it is not working with upgraded version:

    Outbound rules

    inbound rules

    Please note that all the razor pages are at root level and not in Pages folder. Any suggestions to make it work?

    Thanks in anticipation.

    Sunday, July 7, 2019 5:55 PM

Answers

  • User-821857111 posted

    As I said, the routing rule that you created ({*url}) will result in every request being handled by the index page. So when you try to post to another page, it doesn't work. The index page handles the request. You would only ever create that kind of rule if you intended that your index page intercepts every request. If that was not your intention, you should remove the AddPageRoute line that you added to your configuration. Then requests can be routed to other files.

    How many pages does your application have?

    [EDIT]

    Actually, ignore that. It looks like your home page has multiple optional parameters. You can probably just add a route template to the @page directive that caters for that and allows routing to map urls to other files. You need something like:

    @page "{param1?}/{param2?}/{param3?}/{param4?}"

    You might need to add another route to cater for your pagenumber parameter, using a constraint to ensure no ambiguity. You would use the AddPageRoute method to do that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 12, 2019 1:43 PM
  • User-1764593085 posted

    Hi asimnazir123,

    If you want to post data to other razor pages, you could use `asp-page` to set razor pages, `asp-page-handler` to set redirected hanlder and `asp-route-{parameter}` to pass data

    <form method="post" asp-page="AddToBasket" asp-route-ssn="@Model.SSN" asp-route-product="@Model.product" id="form2">

    And in AddToBasketModel, you could access the data in post handler:

    public void OnPost()
            {
                var ssn = Request.Query["SSN"];
                var prodcut = Request.Query["Product"];
    
            }

    Refer to https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-2.2&tabs=visual-studio#write-a-basic-form

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 16, 2019 8:56 AM

All replies

  • User-821857111 posted

    First, move your Razor Pages to the Pages folder. Then, if you want them to be found with URLs that differ from the file names, provide route templates to the @page directive.

    https://www.learnrazorpages.com/razor-pages/routing

    Sunday, July 7, 2019 6:42 PM
  • User55524441 posted

    OK. So, after moving razor pages to Pages folder, the application will work fine based upon the IIS rules or?

    Monday, July 8, 2019 7:19 AM
  • User-1764593085 posted

    Hi asimnazir123,

    Please note that all the razor pages are at root level and not in Pages folder.

    By default, Razor Pages are rooted in the /Pages directory. Add WithRazorPagesAtContentRoot to AddMvc to specify that your Razor Pages are at the content root (ContentRootPath) of the app:

    services.AddMvc()
        .AddRazorPagesOptions(options =>
        {
            ...
        })
        .WithRazorPagesAtContentRoot();

    Refer to https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-2.2&tabs=visual-studio#specify-that-razor-pages-are-at-the-content-root

    Best Regards,

    Xing

    Monday, July 8, 2019 7:48 AM
  • User-821857111 posted

    asimnazir123

    OK. So, after moving razor pages to Pages folder, the application will work fine based upon the IIS rules or?

    I don't know what your IIS rules are. You need to either copy and paste them here or upload an image to a public image hosting service. 

    But basically, as it says in the page I linked to, routes for pages are generated according to convention (the file name without the extension) or by configuration. There are many ways to configure routes for pages. They are detailed in the article.

    The config options are very powerful. It's unusual to need to resort to IIS rewriting rules for a Razor Pages application. 

    Tuesday, July 9, 2019 2:04 PM
  • User55524441 posted

    Hi,

    Thanks for your response. Actually, I did attached those rules in my initial thread. If you open the two images (inbound rules, outbound rule) by right clicking and opening in new window then you will be able to see those rules.

    Here are links to rule images:

    https://drive.google.com/open?id=1eGAgrlt1F062AkwSccfSpciRM1KsPlut

    https://drive.google.com/open?id=1THxi1CRwmRFnTAzEMXnT_O_CITyTSAMz


    Thanks!

    Wednesday, July 10, 2019 6:51 AM
  • User-821857111 posted

    If you open the two images (inbound rules, outbound rule) by right clicking and opening in new window then you will be able to see those rules.
    Ah. All I saw was missing images.

    As I said before, I don't think you need those rules at all. You can specify multiple routes for an individual page using attribute routing as detailed in the article I linked to. Have you looked at it yet? Is anything unclear there?

    Wednesday, July 10, 2019 9:55 AM
  • User-474980206 posted

    asp.net core razor pages are different than the webform version.

    in webforms, the razor page is a text file, that is compiled on request, and you reference the page by its IIS file name.

    in asp.net core razor pages are not referenced by their file name. routing is used to access a asp.net core razor page. by default razor page routing is based on the the path to the razor page from the root folder (default /pages) not including the extension. but you can use attribute routing to override.

    as suggested you should remove all the IIS rules.

    Wednesday, July 10, 2019 4:46 PM
  • User55524441 posted

    Mikesdotnetting

    As I said before, I don't think you need those rules at all. You can specify multiple routes for an individual page using attribute routing as detailed in the article I linked to. Have you looked at it yet? Is anything unclear there?

    Ok, great. I was able to handle most of the scenarios using : 

    services.AddMvc().AddRazorPagesOptions(options => options.Conventions.AddPageRoute("/index", "{*url}"));

    and now there is no IIS rule at config level. Thanks for your help.

    Can you please help me in posting a form to another razor page? In .Net v4.0 version it was done like this:

    <form  method="post" action="/basket-add?ssn=@Model.stock.SSN&product=@Model.product" id="form2">
    
    the form is posted through an input:
    <input name="addToCartButton" type="submit" id="addToCartButton" value="Add to Basket" rel="nofollow" />

    I tried few options in core 2.2 but no success. here are examples: <form method="post" asp-page="/basket-add?ssn=@Model.stock.SSN&product=@Model.product" id="form2">
    and
    <form asp-page="/basket-add?ssn=@Model.stock.SSN&product=@Model.product" id="form2">

    Any suggestions?

    Thanks

    Friday, July 12, 2019 12:53 PM
  • User-821857111 posted

    Ok, great. I was able to handle most of the scenarios using : 

    services.AddMvc().AddRazorPagesOptions(options => options.Conventions.AddPageRoute("/index", "{*url}"));

    You've created a catch-all route. I don't know whether that was deliberate, but the consequnce is that all requests will be handled by your index page.
    Can you please help me in posting a form to another razor page?
    Yes. Remove the catch-all route.

    Friday, July 12, 2019 1:07 PM
  • User753101303 posted

    Hi,

    I wonder if you shouldn't do that in two steps. It seems there is a quite major design issue about routing as for some reason IIS was/is heavely involved rather than configuring that from within the app as usually done.

    I would likely start by fixing this in the current version (and maybe some other design issues) and then only I would to the port (so that I can focus on the migration rather than having both to migrate and fix design issues).

    Note sure what those inbound and outbound rules were trying to do....

    Friday, July 12, 2019 1:17 PM
  • User55524441 posted

    Yes. Remove the catch-all route.

    I am not facing this issue at Index page and instead it is some other page.

    Friday, July 12, 2019 1:19 PM
  • User-821857111 posted

    As I said, the routing rule that you created ({*url}) will result in every request being handled by the index page. So when you try to post to another page, it doesn't work. The index page handles the request. You would only ever create that kind of rule if you intended that your index page intercepts every request. If that was not your intention, you should remove the AddPageRoute line that you added to your configuration. Then requests can be routed to other files.

    How many pages does your application have?

    [EDIT]

    Actually, ignore that. It looks like your home page has multiple optional parameters. You can probably just add a route template to the @page directive that caters for that and allows routing to map urls to other files. You need something like:

    @page "{param1?}/{param2?}/{param3?}/{param4?}"

    You might need to add another route to cater for your pagenumber parameter, using a constraint to ensure no ambiguity. You would use the AddPageRoute method to do that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 12, 2019 1:43 PM
  • User55524441 posted

    OK. There are around 40 pages and default.cshtml is designed such that it processes large number of pages depending upon different conditions.

    Friday, July 12, 2019 1:56 PM
  • User-474980206 posted
    You will need to convert your IIS inbound routing rules to razor page routes. Assuming the outbound match the inbound, you only need to do the inbound rules.

    Friday, July 12, 2019 3:48 PM
  • User753101303 posted

    IMO what you shown is NOT what you really have because of outbound rules. It seems the intent was to move from url query string values to url segments without updating the app ? You want use segment based values as well in ASP.NET Core ?

    Friday, July 12, 2019 4:00 PM
  • User55524441 posted

    Yep. I need to handle both QueryString as well as Segments i.e. values in URL could be

    index?val=stitching

    or

    index/stitching

    Friday, July 12, 2019 7:33 PM
  • User55524441 posted

    asimnazir123

    Can you please help me in posting a form to another razor page?

    Yes. Remove the catch-all route.

    Here is sample project where there is no catch-all rule but still I am not able to post to another page. Please note that in the project, you need to go to View Basket and then using submit button form needs to be posted to AddToBasket razor page.

    https://drive.google.com/open?id=1F21bDAvsx-TzjhpqECGyY-29a5L8-Zqr

    Thanks in advance for all of your interest and help!

    Sunday, July 14, 2019 6:36 PM
  • User-1764593085 posted

    Hi asimnazir123,

    If you want to post data to other razor pages, you could use `asp-page` to set razor pages, `asp-page-handler` to set redirected hanlder and `asp-route-{parameter}` to pass data

    <form method="post" asp-page="AddToBasket" asp-route-ssn="@Model.SSN" asp-route-product="@Model.product" id="form2">

    And in AddToBasketModel, you could access the data in post handler:

    public void OnPost()
            {
                var ssn = Request.Query["SSN"];
                var prodcut = Request.Query["Product"];
    
            }

    Refer to https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-2.2&tabs=visual-studio#write-a-basic-form

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 16, 2019 8:56 AM