Good evening,
I have one scenario that i have a httpGet method with complex object and needs to call this from another .net project by passing Json object., Below is the sample.
APIController
[HttpGet]
[Route("GetName")]
public async Task<string> GetName([FromUri]MyClass myClass)
{
return myClass?.MyName?.ToString() + "this is my method result";
}
Postman
http://localhost/api/NameSearch/GetName?MyClass={'MyName':'TestName'}
I know this is very easy to achieve if i change the action from httpGet to httpPost., But this has been told that i am not doing any update within my api, so i should not use the Post. Also i should not be sending this as individual parameter like ?MyName=''&MySecondParam='',etc., The request has to be passed as single Json object to the API.
Please kindly suggest if there is any option. I tried the above ?MyClass={'MyName':'TestName'} which is not working.
Thanks in advance.
Related
experts, I would log every incoming http request call with payload into database.
I checked there would be 2 approaches.
use filter or interceptor.
I feel filter is so easier for me to implement.
what would be best approach for my purpose?
please kindly advise.
thank you very much!
if you have a need to do something completely generic (e.g. log all requests), then a filter is sufficient - but if the behavior depends on the target handler or you want to do something between the request handling and view rendering, then the HandlerInterceptor provides that flexibility.
But anyway, just do the way which make you feel easily and simply.
Note:
Interceptor work in spring application context
Servlet work in web context
Use Spring AOP. Use any advice according to your needs.
#Aspect
#Component
public class Test {
#Around("#annotation(mapping) ")
public Object preAuthUserPersmission(ProceedingJoinPoint joinPoint, RequestMapping mapping) throws Throwable {
Object[] parameters = joinPoint.getArgs();
// Your actions on the input parameters
return joinPoint.proceed(joinPoint.getArgs());
}
}
I am converting code that was written in ASP.NET MVC to ASP.NET Core MVC. While I was converting the code, I encountered a problem. We used a method that has multiple parameters like this:
[HttpPost]
public class Search(List<int> ids, SearchEntity searchEntity)
{
//ASP.NET MVC
}
But when coding this in .NET Core, the ids parameter is null.
[HttpPost]
public class Search([FromBody]List<int> ids,[FromBody]SearchEntity searchEntity)
{
//ASP.NET Core MVC
}
When I place the ids parameter in the SearchEntity class, there is no problem. But I have lots of methods that are written like this. What can I do about this problem?
Can only have one FromBody as the body can only be read once
Reference Model Binding in ASP.NET Core
There can be at most one parameter per action decorated with [FromBody]. The ASP.NET Core MVC run-time delegates the responsibility of reading the request stream to the formatter. Once the request stream is read for a parameter, it's generally not possible to read the request stream again for binding other [FromBody] parameters.
MVC Core is stricter on how to bind model to actions. You also have to explicitly indicate where you want to bind the data from when you want to customize binding behavior.
I have used a solution where multiple parameters are sent as [FromBody] using a tuple:
[HttpPost]
public class Search([FromBody](List<int> ids, SearchEntity searchEntity) parameters)
{
//ASP.NET Core MVC
}
I have a project using spring boot framework. From what I understand of the current code base the API's are designed in a way to work with the web browser. For Example
#RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(#ModelAttribute("userForm") User userForm, Model model, RedirectAttributes redirectAttributes){
// Login code
return "redirect:/home"
}
Is it possible to modify this code base to that we return a JSON instead of the redirection? My ultimate goal is to send have a mobile application making the requestion to this endpoint. Is it possible to modify this code base or I have to create a new endpoint for mobile application.
Please excuse me if this sounds like a stupid question. I have no prior experience with spring boot and trying to understand my options
Thank You.
To provide details regarding my platform. I am using Visual Studio 2017 with a .NET Core Web project.
I am implementing a new back-end for a client for which I cannot alter the client. I am attempting to use MVC to accomplish this.
The URL which I must be able to respond to is -> https://my.server.com:8443/portal/gateway?sessionId=0A6405100000001D04FB55FE&portal=4ba457d0-e371-11e6-92ce-005056873bd0&action=cwa&token=3fcbc246bb3c35a5fa8055fec6bbf431
I would like to extract the values for :
sessionId
portal
action
token
As they all have meaning.
I have created a new view folder within my project called "Portal" and have created a new MVC controller named "PortalController".
I have created a new View called gateway.cshtml and also have implemented the following function ->
public IActionResult Gateway(string sessionId, string portal, string action, string token)
{
ViewData["Message"] = "Gateway";
return View();
}
When I set a breakpoint within the function, I receive the following values for the URL provided above ->
sessionId "0A6405100000001D04FB55FE" string
portal "4ba457d0-e371-11e6-92ce-005056873bd0" string
action "gateway" string
token "3fcbc246bb3c35a5fa8055fec6bbf431" string
As can be seen in the output of the debugger, for the parameter named "action", isntead of receiving the value "cwa" as passed by the client, I instead receive the name "gateway" which is passed by MVC as the name of the action.
Is there a "proper way" of handling this?
I would prefer to avoid altering the routes within the startup.cs file as this hurts the modularity of the code. I would welcome attributes that could be used to reroute the parameter names.
Another alternative I could see (buy can't figure out) is to simply make use of the HTTP request to read the values I'm interested in instead of using function parameters.
Thank you very much in advance for any assistance you can provide!
In .Net Core you can get your action parameter value straight off the Request with
var action= Request.Query["action"];
where in .Net Framework it was
var action= Request.QueryString["action"];
Please help me to find a solution for the titled problem. I've a Webservice developed in C# with few action. For each action there are two XSDs one for request and another for response. I want that when service is invoked on the client side these XSDs shold get exposed into the wsdl document. Any help to achieve this is most welcome.
You can make changes to webMethods? If yes, you can decorate the methods by the attribute XmlAttributeInclude.
This should include the declaration of XYZ and ABC types in the WSDL of your Web service. I hope I have understood your needs.
[XmlInclude(typeof(XYZ))]
[XmlInclude(typeof(ABC))]
[WebMethod()]
public XYZ Foo(ABC data)
{
return new XYZ();
}