About .net core handler method routing - ajax

About .net core handler method routing
Calling PageModel's handler method with ajax.
At this time, please tell me how to set the routing for each handler method
I want to set in handler method units instead of page units
PagemodelName:IndexModel
handler method name:OnPostGetDataAsync
An example
$.ajax({
type: "POST",
url: '/Index?handler=GetData',
I want to change to the following
url: '/test/Index?handler=GetData'
Please tell me how to implement to achieve this routing.
After adding the Route attribute, the following warning will be displayed, so I would like to implement it by a method other than adding the Route attribute([Route("test")])
RouteAttribute cannot be applied to Razor pages.......

Related

getUserByToken at auth.service call double

I'm try using metronic today, and see that method getUserByToken at auth.service always call double, it is normal or some bug? because when we start using ajax post to populate user detail, this will call same ajax 2x.
I'm using metronic 7 with angular.
I'm found root cause why this always double ajax
auth service constructor called
auth.service.ts:72 getUserByToken called
app.module.ts:23 APP_INITIALIZER appInitializer(authService: AuthService) called
auth.service.ts:72 getUserByToken called
in app module define APP_INITIALIZER to check token, and in constructor of class already create subscribe. But I don't know while I remove subscribe at constructor effect.

How to call the WebApi,Implemented with AttributeRouting for each action methods,calling it from my client and passing the Query parameters in url?

I have implemented attribute routing for each of action methods in My Webapi.
Example of action method is:-
[Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")]
[HttpDelete]
public HttpResponseMessage DeleteDocument(int fileInfoId, string customerAccountName)
{
//***
//*** Some body contents
//***
}
Now i want to call the above action method from the client example( Fiddler Web debugger),or browser and want to pass the Url request in the below pattern:-
http://{localhost:9791}/api/DocumentApi/DeleteDocument?fileInfoId=12&customerAccountName="Manish"
Currently i am not able to hit the above action method by the above specified url request.
But if i use the url pattern like below:-
http://{localhost:9791}/api/DocumentApi/DeleteDocument/12/Manish
I am able to hit the above action method.But for my project requirement,I need to use the Url with query parameters only.
Please suggest me the approach,how to achieve this?
Any response will be greatly appreciated.
Route templates in Web API do not support specifying query string parameters. For your scenario, do not define fileInfoId and customerAccountName as part of your route template as doing so makes Web API to strictly look for 5 segments(the text between the / characters in your route template) in your request url...so just modify your route template to [Route("api/DocumentApi/DeleteDocument")] and keep the parameters on the actions as it is...
Use like following code :
[Route("api/DocumentApi/DeleteDocument/{fileInfoId}/{customerAccountName}")]
public HttpResponseMessage TestAction(string fileInfoId,string customerAccountName)
{

Change Action to be executed on ASP.NET WebAPI with ActionFilterAttribute

My goal is to develop a Custom FilterAttribute, there i can apply to an action on ASP.NET Web API, where one of two thing should happens, but the resul must be the same:
Expected result: if some condition is verified, the action execution should be cancelled or redirected and a different object should be returned;
on a filterattribute to mvc i just have to change the actionname when im overriding a specific method.
does any one have ideas how to do the same on a webapi filterattribute?
SOLUTION:
if (true)
{
IHttpRouteData x = request.Properties["MS_HttpRouteData"] as IHttpRouteData;
x.Values["action"] = "IsUnavailableBecause";
}
I'm not sure I understand what you are trying to do. Can you give an example?
Using message handlers would allow you to change the action route parameter before action selection has occurred.

WebApi ApiExplorer ApiExplorerSettingsAttribute and IgnoreApi for Action Parameters

You can decorate a controller or action method with the ApiExplorerSettingsAttribute setting the IgnoreApi property to NOT generate help info. If you try to apply the same to an action method's attribute, you get an error:
public HttpResponseMessage Post([ApiExplorerSettings(IgnoreApi = true)]HttpRequestMessage request, ... )
Error 2 Attribute 'ApiExplorerSettings' is not valid on this declaration type. It is only valid on 'class, method' declarations.
A common convention for keeping your controller actions testable is to accept an HttpRequestMessage parameter, but this is an implementation detail, not something your API consumers should know about.
How can I prevent the ApiExplorer from including this parameter when it generates the help page?
For clarity...by this "if you try to apply the same to an action method's attribute", did you mean you were trying to apply to the parameter?
-A quick fix would be is to add an additional check to the existing CancellationToken check that we have in this file: \Areas\HelpPage\Views\Help\DisplayTemplates\Parameters.cshtml
// Don't show CancellationToken because it's a special parameter
if (!typeof(CancellationToken).IsAssignableFrom(parameter.ParameterDescriptor.ParameterType))
{
-Also, you could avoid having HttpRequestMessage as a parameter on action as you can get the current request from the Request property on the controller, but you wanted it as a parameter for testing...is it?

Run function on event

im trying to run javascript function when other funcion will stop.
For example: User change something in form and ajax has been sent in background. When ajax request is done i want to run function.
How can i do that ?
Or maybe there is a global trigger for ajax requests ?
You could achive that thanks to ajax asynchronous request and handler. If you, for example, call via ajax a page that do a particular function and then return, you can run your js.
Take a look at this: http://api.jquery.com/ajaxComplete/
You can configure a handler for the ajax request which gets called when you get response for the request. You can check the examples at http://api.jquery.com/jQuery.ajax/
If you want to call a specific function for a specific AJAX call, simply call it as part of the success or complete callbacks on that AJAX call. If, however, you want to call the same function whenever any AJAX request is finished, take a look at the .ajaxSuccess() or .ajaxComplete() methods in the jQuery API.
It looks that you are using jQuery, so you can use the "complete" or "success" settings to call code/function. Check the docs.
$.ajax({
url: "test.html",
success: function(){
// call another function
}
});

Resources