does anyone know how to pass a URL as a query string parameter and then get the URl in HttpGet method as a parameter ?
Thanks so much for all the answers. Finally I got it sorted. Please refer to my fix below:
[Route("api/[controller]")]
public class UrlController : Controller
{
[HttpGet("{*longUrl}")]
public string ShortUrl(string longUrl)
{
var test = longUrl + Request.QueryString;
return JsonConvert.SerializeObject(GetUrlToken(test));
}
just like this?
current url with UrlEncode
[HttpGet]
public ActionResult Index(string url = null)
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
HttpUtility.UrlEncode should do the job for you
404 might be because the application is not in running mode
host you application and try it in local it should be working as needed
i was trying as http://localhost:11331/Home/?id=http%3A%2F%2Flocalhost%3A11331%2F
and everything is working fine even the redirection to the launch screen
or else post the complete URL you are getting so that i can help you
Related
Sorry for the title, but don't know how to explain it. (.NET ASP MVC)
So what I'm trying is to create a payment request via TripleA API(redirect on their page), if the payment is successful, they will redirect on my success page with some parameters, how can I handle those parameters?
What I've tried:
public IActionResult ErrorPage(string payment_reference, string status)
{
return View(payment_reference,status);
}
https://developers.triple-a.io/docs/triplea-api-doc/dd286311a5afc-make-a-payment-request
(scroll down to success_url for more info)
To expand on Steve's comment, create a record (less code than a class) as follows...
public record ErrorViewModel(string PaymentReference, string Status);
...then use this when you send data to the view...
public IActionResult ErrorPage(string payment_reference, string status)
{
return View(new ErrorViewModel(payment_reference,status));
}
You'll need to update your view to have the following line at the top...
#model ErrorViewModel
That should be all you need.
Based on the documentation, you expect a request like this,
https://www.myshop.com/payment-success?status=paid&payment_reference=ASDDF...&order_currency=USD&order_amount=10
And you translate that into a controller method,
[HttpGet("payment-success")]
public IActionResult ResultPage(string payment_reference, string status, string order_currency, decimal order_amount)
{
var result = new ResultViewModel(payment_reference,status, order_currency, order_amount);
return View(result);
}
I also noticed that the doc says,
Note: This field is required if integrating using External URL Payment Form. For other integrations, either insert the field with a url, or remove the field completely.
So if you use External URL Payment Form integration, then I don't think you will be able to get the status and reference.
The same applies for cancel_url.
I am working on Github api Oauth.
The main problem is to match callback URl with method in Web Api
[HttpGet]
[Route("api/values/callback/{code}&{state}")]
public JsonResult Get (string code, string state)
{
var s = User.Claims;
return new JsonResult(s);
}
In StartUp file:
options.CallbackPath = new PathString("/api/values/callback/");
Redirect in URL that should match an action in service
http://localhost:5000/api/values/callback/?code=b1b0659f6e0&state=CfDJ8Ir-xT
So, I can not build rout which has pattern /?param1¶m2
Here is redirect URL that should match :
Would be glad for help :)
You can use [FromQuery] to get values from the query string.
[HttpGet]
[Route("api/values/callback")]
public JsonResult Get([FromQuery]string code, string state)
{
string s = "code:" + code + " state:" + state;
return new JsonResult(s);
}
Test
If you're using OidClient you can use the following to inject the service to the method and get the querystring to process.
// route method here
public async Task<ActionResult> ProcessCallback([FromServices] HttpListenerRequest request){
...
// generate the options using `OidcClientOptions`
var client = new OidcClient(options);
var result = await client.ProcessResponseAsync(request.QueryString.ToString(), null);
...
}
I would suggest to edit the route to api/values/callback/code={code}&state={state} .
So i have this ASP.NET Core on my local machine, i have installed the prerequisites and after running the application locally, the response was correct from the web browset that it was not found.
Okay, i am trying to invoked this API via Postman and i couldnt determine why i cant access it though i already checked the routings.
Below is the sample template
[HttpGet]
[Route("Details")]
public async Task<IActionResult> GetDetails(string value = null)
{
var response = new ListModelResponse<SomeModel>() as IListModelResponse<SomeModel>;
try
{
response.Model = await GetDetailsRepository
.GetDetailsSS(value)
.Select(item => item.ToViewModel())
.OrderBy(item => item.Name)
.ToListAsync();
}
catch (Exception ex)
{
response.DidError = true;
response.ErrorMessage = ex.Message;
}
return response.ToHttpResponse();
}
And in application insights of visual studio, i can see that it is invoking the API but it seems it can't get through.
Check this insights snippet
Other API's are working fine, it seems that i am missed something that i can't figure out right now.
For the routing i have this.
[Route("api/[controller]")]
I have also checked the Token and as i parsed it, i am getting the required info to access this API.
Thanks Lads!
It doesn't seem that you have the name of the controller in the request url.
api/[controller]/Details?value=CAT...
This error is due to the incorrect url present in the request. The correct URL has to be https://localhost:44309/api/your-controller-name/Details?value=CAT
ie. If the Controller name is ProductsController, then the URL has to be https://localhost:44309/api/Products/Details?value=CAT.
[Route("api/[controller]")]
public class ProductsController : Controller
{
[HttpPost("Details")]
public async Task<IActionResult> GetDetails(string value = null)
{
...
}
}
i just started learning asp.net core api and i'm trying to make a post request from Postman. I want to return a status code of 201 upon post and return the Uri of the newly posted data as the location. Below is my controller code
[HttpPost]
public ActionResult CreateMake([FromBody]MakeResource makeresource)
{
if (!ModelState.IsValid) {
return BadRequest(ModelState) ;
}
var makes = mapper.Map<MakeResource, Make>(makeresource);
_makes.AddMake(makes);
makeresource.Id = makes.Id;
return Created(new Uri(Request.Path, UriKind.Relative), makeresource);
}
I get an error
System.UriFormatException: 'Invalid URI: The format of the URI could
not be determined.'
and I guess it has to do with the Uri(Request.Path).
In Asp.net web api, I would implement it as return Created(new Uri(Request.Uri + "/" + makes.Id), makeresource); and the action type would be IHttpActionResult instead of ActionResult as shown in my controller but it doesn't work that way in asp.net core.
Any assistance on how to solve this issue would be needed.
Thanks.
I just want to put this here, incase anybody needs it in the future.
I got the answer i used from What is the ASP.NET Core MVC equivalent to Request.RequestURI?
I solved my issue by replacing (Request.Path, UriKind.Relative) with as (Request.GetEncodedUrl()+ "/" + makes.Id) seen below:
return Created(new Uri(Request.GetEncodedUrl()+ "/" + makes.Id), makeresource);
I want get file url request in controller .
when come this request
http://localhost:4616/Images/Users/mohammadhossein21#gmail.com/hm2.jpg
or this request
http://localhost:4616/Images/Users/mohammadhossein21#gmail.com/4230.pdf
I can get this request in The following action of Images Controller
public ActionResult Users(string Email, string Name)
{
string subPath1p = Directory.GetParent(Server.MapPath("~/")).Parent.FullName + "\\SUPPORT_USERSFILES\\" + Email+"\\"+Name+".pdf";
return File(subPath1p, "application/pdf");
}
You should customize routing: see here or here for example.
Instead of ActionResult, use FileResult as return type.