0
votes

I have been stuck with two problems in ASP.NET CORE RAZOR PAGES and I didn't find any content about it.

Problem 1: I have been trying to do an overload on my page component, but it looks like it's not possible, to make it works, I'm, using a nullable parameter on my method, but I do not know if this is the best way to do it, there's a better way?

What I have working:

// For New Item or Edit Item

public IActionResult OnGetModal(int? squid)

What I wanted:

// For new Item

public IActionResult OnGetModal()

// For Edit Mode

public IActionResult OnGetModal(int squid)

NOTE: If instead this unique method with a nullable parameter, I use two methods 1 method without parameter and another method with this int parameter, I got an error because I have multiple handlers.


Problem 2: I have 2 methods with the same parameter name, and I got an error when I try to refer to this handler.

Method 1:

public IActionResult OnGetModal(int? squid)

Method 2:

public bool OnPostActivateDeactivate(int squid, bool isActive)

Here I'm trying to access the method Modal:

URL: ?handler=modal&squid=1001

Error: InvalidOperationException: Unsupported handler method return type 'System.Boolean'.

How do I solve these problems or what would be the best way to do it?

2
Your question is confusing. You need to clarify it.Lewis86
@Lewis86 I'm trying to do an overload on my PageModel, but overload doesn't work.Natan Dutra
@Lewis86 I also created ANOTHER METHOD on my PageModel that contains the same parameter name than my previous method, I mean, I had a method called OnGetModal(int? squid) that was working fine, after that I created a new method called OnPostActivateDeactivate(int squid, bool isActive) and after that, my previous method stopped to work because it says that I'm missing the bool parameter (THAT IS REQUIRED ONLY ON MY SECOUND METHOD ):/Natan Dutra
You can not access actionResult Method because its return View not a values. You need to create another MethodKiran Joshi

2 Answers

1
votes

First, you cannot have multiple handlers with the same name, differentiated only by parameters in the same page. You need to choose different names.

Second, the only permissible return types for handler methods are IActionResult, void, Task and Task<IActionResult>. If you want to return the value "true" from a handler, return it as a string in a ContentResult, or as a JsonResult.

1
votes

For Problem 1, this is by design and this is controlled by IPageApplicationModelProvider and IPageHandlerMethodSelector, it distinguishs the hanlder by httpMethod and handlerName. You could implement your own IPageHandlerMethodSelector by code below:

        //Setp 3: remove candidates with non-matching parameters
        for (var i = candidates.Count - 1; i >= 0; i--)
        {
            var handler = candidates[i];
            var request = context.HttpContext.Request.Query.Count - 1;
            if (handler.Parameters.Count != request)
            {
                candidates.RemoveAt(i);
            }
        }

You could register your own IPageHandlerMethodSelector like below:

services.AddSingleton<IPageHandlerMethodSelector, CustomDefaultPageHandlerMethodSelector>();

Here is complete CustomDefaultPageHandlerMethodSelector.

For Problem 2:, you need to pass RequestVerificationToken like below:

    <script type="text/javascript">
    $(document).ready(function () {
        var postSubmit = $.ajax({
            type: "POST",
            headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
            url: "testmodel?handler=ActivateDeactivate&squid=1&isactive=true",
            //data: JSON.stringify({ ... }),
            contentType: "application/json"
        }).done(function (response) {
            //...
        });
    });
</script>