In one of my controllers, I do have a return that looks like this:
return RedirectToAction("AdministerFiles/ViewDataFiles?catid=14");
but when it renders the result to the browser the string becomes this one:
AdministerFiles/AdministerFiles/ViewDataFiles%3fcatid%3d14
how can I solve this ? thanks .
You just need the action as the parameter (along with the route data):
return RedirectToAction("ViewDataFiles", new { catid = 14 });
If you want to specify the controller as well (it defaults to the current controller), then you can do it like this:
return RedirectToAction("ViewDataFiles", "AdministerFiles", new { catid = 14 });
Related
I ad trying to pass parameters to call the following C# method
public ActionResult GenerateInvoicePDFByInvoiceNum(string id,string apikey)
{
IInvoiceRepository rep = db.GetInvoiceRepository();
//
var api = Guid.Parse(apikey);
Invoice invoice = rep.GetByExpression(i => i.InvoiceNo.Equals(id) && i.Visit.Branch.Practice.APIKey.ToString().Equals(apikey)).FirstOrDefault();
if (invoice==null)
{
return HttpNotFound();
}
//return new Rotativa.ActionAsPdf("PrintInvoice", new { id = invoice.Id })
//{
// //CustomSwitches = "--load-error-handling ignore "
// CustomSwitches = "--disable-javascript"
//};
return RedirectToAction("GenerateInvoicePDF", new { id = invoice.Hash });
}
From the browser I am trying to call it with a call like this which worked when I had only one parameter, but I don't know how to change those to pass the second parameter
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d
Thanks
On your url: http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d
"/72341d" represents an optional parameter.
Try to pass a query string.
To do that, you need to specify the parameter name and value you want to pass.
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=72341d&apikey=YOUR_API_KEY
This is how you pass parameters by url:
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=72341d&apikey=yourapikeygoeshere
You can pass multiple parameters as "?param1=value1¶m2=value2"
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=1&1pikey=2
You have three choices to do this:
Pass parameters using query string like /?id=value&apikey=value
Add a custom route in "RouteConfig" (~/App_Start/RouteConfig.cs) like below:
routes.MapRoute(
name: "RouteName",
url: "{controller}/{action}/{id}/{apikey}"
);
Use attribute routing. To do this first enable it by calling the "routes.MapMvcAttributeRoutes()" method in "RouteConfig" and then apply the [Route(urlPttern)] attribute to the related action and pass the url pattern to the method
after applying solution 2 or 3 you can pass parameters in url like:
"/foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d/apikeyvalue"
This kind of question has been asked before on SO (post 1, post 2, post 3), but none of them was able to help me.
I have the following route (defined inside a LandingController class):
[HttpGet]
[Route("connections/{city:name}/map{page?}", Order = 2)]
public Task<ActionResult> Connections(string city, int page = 1)
for which I am trying to generate an URL in my Razor template like this:
#Url.Action("Connections", "Landing", new { city = "madrid", page = 1 })
or
#Url.Action("Connections", new { city = "madrid", page = 1 })
Both these calls return null. Anyone has an idea why?
However if I omit the page parameter in the route template (doing [Route("connections/{city:name}/map", Order = 2)]), the following URL is generated: /connections/madrid/map/?page=1, which is not what I want, but it shows that I am doing something wrong with my calls above.
[HttpGet]
[Route("connections/{city:name}/map{page?}", Order = 2)]
public Task<ActionResult> Connections(string city, int page = 1)
Pretty sure your routing is incorrect, {city:name} what is name? I don't recall any having a constraint type name.
Your attribute routing should be as simple as this:
[Route("connections/{city}/map/{page?}", Order = 2)]
If you want to add constraint to page as integer it would be:
[Route("connections/{city}/map/{page:int?}", Order = 2)]
I am mapping this route:
routes.MapRoute(
"VotePage",
"{category}/{medicine}/{id}-{condition}",
new { controller = "Reactions", action = "Test" }
);
However when I have an url after the last slash like this:
"/8438-this-is-a-test-condition"Than the parameters are set like this:
id = 8438-this-is-a-test
condition = condition
Whilst the correct result should be:
id = 8438
condition = this-is-a-test-condition
How to split/seperate the url on the first dash?
Thanks in advance!
If I understand you correctly, this should work
String foo = "/8438-this-is-a-test-condition";
if(foo.StartsWith("/")
{
foo = foo.Split('/')[1];
}
EDIT:
.... no I did not understand you correctly. Sorry about that, i'll read more carefully next time.
I'm not sure how to achief what you ask for. I'd probebly just go for some find and replace magic, not pretty but it works....
Not acceptable to do it this way?
"{category}/{medicine}/{id}/{condition}"
Here's the jquery from my view:
$("#btnSelect1").click(function () {
var donationTypeID = $(this).closest('p').find('#selectList').val();
var id = parseInt(donationTypeID);
var id2 = $('#person').val();
var persId = parseInt(id2);
// var personName
var route = '/Donation/ContinueDonation/' + id + '?personId =' + persId;
$("#donationSection1").load(route, function () {...});...
Here's the controller method
public ActionResult ContinueDonation(int id, int personId)
{}
Any thoughts on how I can get this to work? Thanks
I actually think the previous answer is not all that right. Your query string is correct if it were hitting the default route established by MVC which matches a URL something like this {controller}/{action}/{id}.
What must really be going on is that you messed with the routes and your query is either not hitting the default route or you changed that.
What I would do is get the RouteDebugger (just get it via nuget) and see which route your query string hits. You could setup a route for any URL you want it to be (sort of the whole point of friendly URLs), so If you want the route to be /ContinueGiving/{id}/{personId} you can do that by adding a route to the beginning of the routes getting added with a definition something like this:
routes.MapRoute(
name: "ContinueDonation",
url: "ContinueGiving/{id}/{personId}",
defaults: new { controller = "Donation", action = "ContinueDonation" },
constraints: new { id = #"\d+", personId = #"\d+" }
);
Any parameters you do not specify will get mapped if the framework can find any matching items in either posted values, the query string, etc.
The constraints make sure this route only gets matched if the parameters passed are numbers.
So the previous answer worked because somehow the route with id didn't match, but that is not really your issue.
You didn't give a key to the id value:
var route = '/Donation/ContinueDonation/?id=' + id + '&personId =' + persId;
BTW, the var persId = parseInt(id2); line is redundant, as every parameter sent with HTTP is a string, so "234" and 234 are exactly the same.
In ASP.NET MVC, is it possible to define routes that can determine which controller to use based on the data type of part of the URL?
For example:
routes.MapRoute("Integer", "{myInteger}", new { controller = "Integer", action = "ProcessInteger", myInteger = "" });
routes.MapRoute("String", "{myString}", new { controller = "String", action = "ProcessString", myString = "" });
Essentially, I want the following URLs to be handled by different controllers even though they have the same number of parts:
mydomain/123
mydomain/ABC
P.S. The above code doesn't work but it's indicative of what I want to acheive.
Yes, if you use constraints:
like so:
routes.MapRoute(
"Integers",
"{myInteger}",
new { controller = "Integer", action = "ProcessInteger"},
new { myInteger = #"\d+" }
);
If you put that route above your string route (that doesn't contain the constraint for #"\d+"), then it'll filter out any routes containing integers, and anything that doesn't have integers will be passed through and your string route will pick it up.
The real trick is that Routes can filter what's coming through based on Regular Expressions, and that's how you can determine what should pick it up.