Ajax.RouteLink gives a 404 - asp.net-mvc-5

I have this link
#Ajax.RouteLink("Bid", RouteNames.Ajax.BidOnLot,
new
{
lotId = Model.Lot.Id,
bidAmount = Model.NextBidAmountForUser
},
new AjaxOptions
{
HttpMethod = "POST",
OnFailure = "OnFailure",
OnSuccess = "OnSuccess"
})
and this action method
[AjaxOnly, HttpPost]
[Route("ajax/bid-on-lot/{lotId}/{bidAmount}", Name = RouteNames.Ajax.BidOnLot)]
[Authorize]
public JsonResult Bid(string lotId, decimal bidAmount)
RouteNames.Ajax.BidOnLot is set to "BidOnLot"
But for some reason when I click on the link, I get a 404 with this message.
404 Not Found - http://localhost:12472/ajax/bid-on-lot/lot-901/210.0
When I have only the LotId parameter everything works normally but adding an additional parameter makes it all fail.
Why is this happening?

Just in case anyone else struggles with this in the future let me tell you what the issue was. Passing a decimal parameter was the issue. Passing an int, double or string is fine but when I made it a decimal it failed.

Related

Debugging a Base64 string

How would I go about debugging what's wrong with a string that I believe is in Base64 format, but which in VB using the below line of code
Dim theImage As Drawing.Image = imageUtils.Base64ToImage(teststring)
throws the following exception?
{"Base64ToImage(): The input is not a valid Base-64 string as it contains a
non-base 64 character, more than two padding characters, or an illegal
character among the padding characters. "}
The test string itself is far too long to paste here; I tried but reached (a lot) over the character limit. I've tried a few online conversion tools and it doesn't seem to work there either. At first I thought I was passing the string wrong from my ajax call to the web method VB code behind...but I've tried hard-coding my string into the function as well with the same failure. So, I'm convinced the string itself is bad data.
It looks like:
Dim teststring = "dataImage/ png;base64,
iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAgAElEQVR4Xuy9268sWbbe9UVE3i /
rvte + V....K/1Tx5/8A736wVclDQN4AAAAASUVORK5CYII="
But I also tried removing the "dataImage" part and using
Dim teststring =
"iVBORw0KGgoAAAANSUhEUgAAB4AAAAQ4CAYAAADo08FDAAAgAElEQVR4Xuy9268sWbbe9UVE3i /
rvte + V....K/1Tx5/8A736wVclDQN4AAAAASUVORK5CYII="
And it doesn't make a difference.
I am getting this string in javascript using this function:
btnFreeze.onclick = video.onclick = function (e) {
e.preventDefault();
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
alert("got here");
$hfLicenseScreenshot.val(canvas.toDataURL());
$img.css("background-image", "url(" + $hfLicenseScreenshot.val() + ")");
$("#hiddenTextbox").val($hfLicenseScreenshot.val());
//$("#save").show();
return false;
};
...where ultimately the string is from
canvas.toDataURL()
and about halfway through that function there is a hidden field called $hfLicenseScreenshot, from which I am saving the value into a "hidden" textbox (I dont know why my variable was getting lost, I know it's redundant but that's why I saved the value to a textbox called hiddentextbox. I get the sstring from hiddentextbox later, like:
$("#hiddenTextbox").val().toString();
So, I have no idea how to go about debugging this image base 64 string. I've tried different images taken from my webcam and it's just not working with any of them. Any ideas?
...I don't know if it's been serialized or not, since I think the JSON stringify method is supposed to do that. I might be confused there.
...Here is my ajax call:
$.ajax({
type: "POST",
url: "/BackCode/FirstPage.aspx/SaveData",
data: JSON.stringify({ currentData: currentData, str: makeblob(str) }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
currentData: currentData,
success: function (resp) {
resp = resp.d;
if (resp.success) {
if (typeof callback === "function")
callback.apply(this, [resp]);
load();
} else {
$.statusMessage(resp.statusMessage, "red");
}
},
error: function (jsonObject, textStatus, errorThrown) {
$.statusMessage(errorThrown, "red");
}
});
I have also been having issues with this, and it goes into the last error function a lot:
$.statusMessage(errorThrown, "red");
So I don't know whether I'm passing it correctly either.
The following works for me:
Dim base64String = "Qk2uAAAAAAAAADYAAAAoAAAABgAAAAYAAAABABgAAAAAAAAAAADEDgAAxA4AAAAAAAAAAAAA////AAAAAAAAAAAAAAAA////AAAAAAD///////////////8AAAAAAP///////////////////////wAA////////////////////////AAD///8AAAD///////8AAAD///8AAP///////////////////////wAA"
Dim base64Bytes = Convert.FromBase64String(base64String)
Using memoryStream = New MemoryStream(base64Bytes)
Dim image As Image = Image.FromStream(memoryStream)
image.Save($"C:\Users\{Environment.UserName}\Desktop\Smile.bmp")
End Using
I've removed the initial metadata which indicates what type of image it is, the original string was:
data:image/bmp;base64,Qk2uAAAAAAAAADYAAAAoAAAABgAAAAYAAAABABgAAAAAAAAAAADEDgAAxA4AAAAAAAAAAAAA////AAAAAAAAAAAAAAAA////AAAAAAD///////////////8AAAAAAP///////////////////////wAA////////////////////////AAD///8AAAD///////8AAAD///8AAP///////////////////////wAA
Try removing the spaces from your base64 encoding - as they're not a valid base64 character - and the metadata from the start.
Here are the valid base64 characters taken from Wikipedia:
I tried removing whitespace like some other suggestions here, and that did not fix my problem (although I'm sure that was likely a secondary problem).
In my case, my string wasn't actually a valid image at all because I had tried to pass it from Ajax to the VB WebMethod as an object (this was because passing as a string was not working). And at one point I had tried converting it to a Blob object....so my test string was just something totally invalid. I should have just left it as a string in both front end and backend. So, I don't even know what my string one that I posted earlier, but it wasn't an image.
Eventually I realized that my string on the front-end was too long, which was my actual/original issue. Now I realize the irony of having mentioned earlier that the string was too long to post in my question. There is something in the WebConfig you can set for maxlength of an image string:
<webServices>
<jsonSerialization maxJsonLength="86753010">
</jsonSerialization>
</webServices>
So, the json serialization was failing because I had this code commented-out. I commented it back in and made the number bigger, and that fixed my issue. So then I was able to pass the string properly and everything worked!

Exception with default value on route

I had a Dto with the following route defined
[Route("/route/{Id}/{Status}")]
public class JustIdAndStatus : IReturn {
public long Id { get; set; }
public long Status { get; set; }
}
Then I invoke the service with something like
localhost:9040/route/1/100
or
localhost:9040/route/0/100
and everything goes well.
but when I use one of the service clients I got an exception if any of the values is 0 (default value for the long).
var client = new JsonServiceClient("http://127.0.0.1:9040");
var request = new JustIdAndStatus() {
Id = 0,
Status = 1
};
var response = client.Get(request);
the exception I get is
System.InvalidOperationException : None of the given rest routes matches 'JustIdAndStatus' request:
/route/{Id}/{Status}: Could not match following variables: Id
at ServiceStack.ServiceClient.Web.UrlExtensions.ToUrl(IReturn request, String httpMethod, String formatFallbackToPredefinedRoute) in UrlExtensions.cs: line 65
at ServiceStack.Common.Tests.UrlExtensionTests.Can_create_url_with_JustIdAndStatus(Int64 id, Int64 status) in UrlExtensionTests.cs: line 91
I tracked down the issue to this commit on the service stack repository
https://github.com/ServiceStack/ServiceStack/commit/b34a5906a265da19d916ea47ee80783bd866abcb
I notice that when I updated from ServiceStack 3.9.38 to 3.9.40 from nuget.
I want to know if this behavior is right, so I'm using the routes in a wrong way or maybe this is an issue and can be submited to the issue tracker on github.
also I make a test using the basic ones I found on ServiceStack.Commons source code
https://gist.github.com/anonymous/5216727
Thanks in advance!
I agree with you that what you describe should be expected behaviour where the property is specifically referenced in the route.
The commit you refer to was intended to suppress the use of default parameter values on routes where members were relegated to the query string. However, it also appears to have caused the issue you describe with explicit routes.
I am putting together a pull request to address the issue and add some tests to reflect the expected behaviour.

service stack wildcard path with swagger

I'm using ServiceStack and have the route like this:
[Route("/hello/{Name*}")]
So anything like /hello/some/parameters should be handled.
Swagger use the following url: /hello/{Name*} and no matter what value I provide for Name parameter I get the following message when I hit "Try it out button":
A potentially dangerous client value Request.Path retrieved from the client(*).
Note, I'm using the following ApiMember attribute:
[ApiMember(Name = "Params", DataType = "string", ParameterType = "path" , IsRequired = true)]
and still any text I input is overriden with * and I have the error as above.
Is there any workaround for this?
First, the error is occurring because the Swagger call is using the default value of {Name*} in the path/request. (*) is not allowed in the path of the URL.
By using the correct attributes in your ServiceStack class, you should be able to get a text box to enter your own value within the Swagger UI.
Something like below should get the text box to appear. See https://github.com/ServiceStack/ServiceStack.UseCases/tree/master/SwaggerHelloWorld for examples.
public class Hello {
[ApiMember(Name=”Name*”, Description = “Name Description”, ParameterType = “path”, DataType = “string”, IsRequired = true)]
public string Name {get; set; }
}

Concatenate string in Asp Mvc

I am using ajax jquery for return a string , i have a entangle , it is Concatenate string
I want concatenate :
string str = "";
str += "<td>"+"<%= Html.ActionLink('Edit', 'ProcessUpdate/' + s.ProductId, 'Stationery')%>"+" </td>";
but when i run application , this is result :
I want to run the program the following results
Edit
thank for all !
It looks your issue is the parameters you are passing to Html.ActionLink(). Your question has been answered
here. The "/" character in your second parameter is not valid since this parameter is the action name in MVC2+ or the controller name in MVC1.
Assuming Stationery is the controller and ProcessUpdate is the action on the controller, your code should look like this:
Html.ActionLink("Edit", "ProcessUpdate", "Stationery" new { Id = s.ProductId }, new { } )
And here is the signature for the action
public ActionResult ProcessUpdate(string id)
{
// Do something
}
Note the last parameter is for Html Attributes and is required for this overload of Html.ActionLink() to work properly.

Kohana 3: How can I pass full control to another action within my controller?

In my controller, I have a before() function that calls parent::before() and then does some additional processing once the parent returns. based on a specific condition, I want to "save" the original request and pass execution to a specific action. Here is my before() function.
public function before() {
parent::before();
$this->uri = Request::Instance()->uri;
$match = ORM::factory('survey_tester')
->where('eid','=',$this->template->user->samaccountname)
->find();
if (!$match->loaded()) {
self::action_tester("add",$this->template->user);
}
}
And the action that is being called..
public function action_tester($op=null,$user=null) {
$testers = ORM::factory('survey_tester')->find_all();
$tester = array();
$this->template->title = 'Some new title';
$this->template->styles = array('assets/css/survey/survey.css' => 'screen');
$this->template->scripts = array('assets/js/survey/tester.js');
$tester['title'] = $this->template->title;
$tester['user'] = $this->template->user;
switch ($op) {
case "add":
$tester = ORM::factory('survey_tester');
$tester->name = $user->displayname;
$tester->email = $user->mail;
$tester->division = $user->division;
$tester->eid = $user->samaccountname;
if ($tester->save()) {
$this->template->content = new View('pages/survey/tester_add', $admin);
} else {
$this->template->content = new View('pages/survey/tester_error', $admin);
}
break;
default:
break;
}
}
This all seems to work fine. This is designed to prompt the user for a specific piece of information that is not provided by $user (populated by LDAP) if this is the first time they are hitting the controller for any reason.
The problem is the views are not rendering. Instead control passes back to whatever action was originally requested. This controller is called survey. If i browse to http://my.site.com/survey and login with new user info, the record gets written and i get the action_index views instead of my action_tester views.
I cannot figure out what I am doing wrong here. Any ideas will be appreciated. Thank you.
EDIT: I managed to get this working (sort-of) by using $this->request->action = 'tester'; but I'm not sure how to add/set new params for the request yet.
The issue is that you are calling your method (action_tester), but then Kohana is still going to call the original action after the before method is called, which is going to change the response content overwriting the changed made in action_tester().
You can change the action being called (after before is called) inside your before() method:
$this->request->action('action_tester');
After the before method is called, it should then call the new Action (action_tester) rather than the old one, but then you need to do something about the way you are passing your parameters then.
Or you could just redirect the request upon some condition:
if($something) {
$this->request->redirect('controller/tester');
}
This doesn't seem like a nice way to do it anyway.

Resources