redirect ajax requests - ajax

i want redirect ALL ajax requests(with X-Requested-With:XMLHttpRequest in header) to action: ajax(string function, string args) . How can i do it?
For example: browser send ajax query with paramerts function=getImage&args=4 to url http://localhost/post/123 but we redirect query to http://localhost/ajax.

Use a custom route and/or route constraint.
Look here for more info.

Probably something like Server.Transfer() would work best

Related

Redirect in controller using RedirectToAction malfunction

Normally google is my best friend but this problem I can't figure out or even understand.
I have an action in my controller that I use for selecting which follow up action in my controller to use, based on user input (see image)
This redirect work like a charm, it's when the action I'm redirected to is finished as the problem arises. In my action I fetch some much needed data for the web site. When tha data is fetched the action are supposed to redirect to a generic action, that in turn will present my view. The "middleware" action redirect correctly and it also send the parameters but the "recieving action" don't handle or recieve the parameters.
I'm totally clueless here, is it not possible to redirect to an action that in turn redirect to another action or what is the problem?
The route specification looks a bit odd.
I think it should be possibly:
[HttpGet("Customer/Device/{id}")]
public IActionResult Device(string id, bool like)
{
}
Now the URL it tried to redirect you to should work. Id from URL, and the like parameter from query.
If you want both in URL:
[HttpGet("Customer/Device/{id}/{like}")]
public IActionResult Device(string id, bool like)
{
}

How to hide function and parameter from url in codeigniter?

this is my URL.
http://localhost/savyy/home/home_page/Islamabad
i want to show it like
http://localhost/savyy/home
i m using uri routing code
$route['home/(:any)'] = 'home/home_page/$1;
but nothings happen...it shows same URL. can anyone please help me ? please tell me how to hide function name and parameter.
I dont think it will work that way, the request is the route to controller. the request uri will be the same. you will still have the "home/home_page/islamabad" in your url. unless you redirect with the home_page() to home() and save the islamabad in a session.

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)
{

Pass hidden value in rewritten URL?

Just wondering if there is a way to pass a hidden variable in a rewritten URL?
My links are working fine with this format:
http://www.mydomain.com/city/state/businessname/
But I'd also like to be able to get and pass the businessID value, but not have it show up in the rewritten link.
http://www.mydomain.com/city/state/businessname/busid/ <- hide busid
If the businessId should not appear in the frontend url then you can retrieve it in the controller / action that is handling this request.
ie. If you can get the businessid from the businessname, there is no need to retrieve it and then rewrite url, just do that logic in your controller action.
Or setup a Filter or Interceptor to handle it.

Route::get function like Kohana in CodeIgniter

Kohana have a smart routing system, I like feature when I can get url by route(Route::get('route_name',params), How implement this feature to codeigniter? Result that I need http://site.com/Sunglasses/Novus/202/ss14.05.html
Thanks
CodeIgniter doesn't support this feature (some would call it "reverse routing").
Maybe I'm not understanding the question properly as I haven't used Kohona, but if you just want to get the uri segment, you would use:
$this->uri->segment(3); //=202 in http://site.com/Sunglasses/Novus/202/ss14.05.html
To route a url with params you would use something like the following:
$route['products/(:any)/(:num)'] = "products/get_product/$1/$2";
so for example with this you could have a url of site.com/products/sunglasses/202 routing to the get_product() method of your products controller and then you would pick up the uri segment as above.

Resources