I am having trouble with building a URL with a query string. I have this code that does what I want it to:
formatoptions: { baseLinkUrl: '#Url.Action("UserInformation", "UserList")', idName: 'Id' }
This makes the proper URL (/UserInformation?Id=4)
This is the section that I am having trouble with, basically trying to replicate what is above, but the syntax is different and I am not sure what's wrong.
results.Add(New SearchResult With {.Link = Url.Action("UserInformation", "UserList", New With {.id = use.Id}), .Text = use.ToString, .Type = "User"})
This make the URL a bit off (/UserInformation/4), it causes problems when redirecting from that page. I'd like to edit this to replicate the proper URL string.
This is a bit of a hack that we figured out to make it work..but I'd like to do it 'properly' if possible
results.Add(New SearchResult With {.Link = Url.Content("~/UserList/UserInformation?Id=" & use.Id), .Text = use.ToString, .Type = "User"})
It's because of your default route having the Id as optional. You can either change the default route which might make all your other Url's look not as clean, or just pick a different parameter name for the Id on the UserInformation action such as userId.
Related
I want to create an url (not a link) for a text field for a share functionality.
Like for example the one stackoverflow uses
I already hava function that produces the path part for the url, like
toUrl : Route -> String
toUrl route = ...
toUrl (Home (Just "hallo")) --> "/?b=hallo"
and using this string for a link as a href attribute works, but I'm wondering how I could create a complete url from this string.
PS: I'm using a single page application so I get an Url at the beginning.
What you say is "the path part for the url" isn't actually just the path part, but the path and query parts of the URL. Ideally you'd separate them so you can create a well-formed URL representation:
{ initialUrl
| path = "/"
, query = Just "b=hallo"
}
But since it's just a record, with no validation, it'll work if you just use it as the path as well. At least if you later just use Url.toString on it. Other operations might cause unexpected results.
{ initialUrl | path = "/?b=hallo" }
I am currently trying to figure out, how to modify the parameter being integrated into the URL Mapping I am using.
static mappings =
{
"/$controller/$action?/$id?/(.$format)?"
{
constraints {
// apply constraints here
}
}
name test1: "/.../$title/..."{
controller = "study"
action = "st_show"
}
name test2: "/.../$title/..."{
controller = "search"
action = "se_show"
}
The parameter $title is pretty much a dataset, which is pulled from a database and which will get transmitted in the following format [ this is a title ]. So there are square brackets in front and behind the string and words are seperated through blanks.
If I am creating a link through g:link now with the params nested in, it gets put into the url as it is pulled from the database. What I am attempting is to create SEO-URLs, which will present a certain title of a publication devided by hyphens instead of url-encoded "%20".
Until now, I was able to generate dynamic urls looking like this:
http://localhost:8080/projectname/show/%5BAllgemeine%20Bevölkerungs[...]/782/...PARAMS...
Furthermore I already implemented it through JQuery, though it should be static and users should be able to copy the link to open up the page themselves - that wouldn't be possible when changing the url client-side while loading up the page.
Is there a way to define a function with something like replaceAll.(' ', '-'), which can be invoked onto the parameter in the mapping to replace blanks with hyphens and f.e. square brackets with an empty character?
That's pretty much, what I wasn't able to come by through the documentation.
Thank you already in advance for your help!
I managed to solve my problem by creating a service with a function containing a regex and executing this function onto the parameter title in my g:link, which I firstly converted to a string, which gets passed to the function.
<g:link controller="study" action="st_show" params="[data: data, ... title: ConversionService.convert(fieldValue(bean: path).toString(), ... data: data)]"></g:link>
And the function in the ConversionService
public static String convert(String title){
title = title.replaceAll("\\s", "-").replaceAll("[^0-9a-zA-Z\\-]", "");
return title;
}
I came across a strange problem in my project today: I use this action throughout my website to generate breadcrumbs for a given page:
#Html.Action("BreadcrumbsWithHeader2", "SharedElements", new { pageName = #Model.pageName, department = #Model.department, menuHeading = #Model.menuHeading, id = "EandTHeader" })
Where it just returns a PartialView. This works great on every page except for one, where it began throwing this error:
No route in the route table matches the supplied values.
I've checked for things like spelling errors, etc., but am not sure how to debug this any further. What could cause something in the route table to go missing?
Edit: I've just noticed that any and all ActionLinks on the website pointing to this page (not the #Html.Action shown above, but rather the view where I call this partial) are producing blank href tags. The controller for it (if this helps) is here:
[Route("JobFair/FindAJobFair/{area}")]
public ActionResult FindAJobFair(string area, string sideMenu)
{
ViewBag.sideMenu = sideMenu;
JobFairsViewModel jobFairInfo = new JobFairsViewModel()
{
department = "Foo",
menuHeading = null,
pageName = "Job Fairs"
};
return View(jobFairInfo);
}
This route is typical of what I use elsewhere on the site (attribute routing).
Do you always provide a value for sideMenu? If you don't that may cause your problem. Try to modify your controller like this:
[Route("JobFair/FindAJobFair/{area}")]
public ActionResult FindAJobFair(string area, string sideMenu = null/*default value*/)
{
ViewBag.sideMenu = sideMenu;
JobFairsViewModel jobFairInfo = new JobFairsViewModel()
{
department = "Foo",
menuHeading = null,
pageName = "Job Fairs"
};
return View(jobFairInfo);
}
Hopefully this answer helps someone in the future - the problem had to do with my variable name area that was passed into the controller. Since my project has an 'Areas' folder it was causing an issue with routing. I simply needed to change area to something like region and the issue was solved.
I have an URL address that I change dynamically I it goes something like this:
The dynamic part is the -> edition = Model.Edition.Usually it as an integer value and the url ends up something like that: ....&edition=1232113 . Sometimes I need it to end up like that : &edition=1232113#10_11 and I managed to pass th right value to the edition placeholder but after the reload it doesn't show the same url that I expected it is similar but it substitutes the '#' with '%23'. And it looks something like that: 1232113%2310_11 and the effect is not what I expect.
From the other side, when I type it manually : 1232113#10_11 it works.
Can you help?
If you problem is concerning that the Url.Action is converting a part of your url, you may want to use the RAW method.
#Html.Raw(Url.Action("Method","Controller", new { Id = Model.DId, dbId = Model.DbId, iconId = Model.IconId, edition = Model.Edition })
I'm localising a site via a Change Language control in the master page. I need to render the control with the current url you're on in each of the different languages.
So if you're on http://site.com/en/Home/About and you change the language to french, I need to direct you to http://site.com/fr/Home/About.
The localisation code works on the route data language property, so I've been trying to figure out how I can:
Get access to the current action (with all original parameters)
Get the url to the current action (with all original parameters) with the route data changed.
Can anyone point me in the right direction?
I've tried passing the ViewContext from the parent into the UserControl, which gives me access to the route data but I can't figure out how to get the language routed url from that.
I ran this on the site I'm working on locally and it seemed to work. There's probably a cleaner way.
HttpRequestBase hrb = HttpContext.Request;
System.Uri url = hrb.Url;
string[] test = url.AbsoluteUri.Split('/');
int nIndex = 0, nCounter = 0;
foreach(string str in test)
{
if (str.Contains("site.com"))
{
nIndex = nCounter;
break;
}
nCounter++;
}
string strLanguage = test[nIndex + 1];
Obviously the +1 can even go in the IF statement, but I didn't think it looked good there. Hope this helps some.
I'm not 100% happy with this, I haven't got to a stage where I can fully test the impact of this but this is what I'm going for so far. Please do answer if you have a better solution.
I pass the ViewContext from the masterpage so I get the ViewContext with route data from whatever url you're currently on.
private string GetLocalisedUrl(ViewContext viewContext, string language)
{
viewContext.RouteData.DataTokens[LANGUAGE_ROUTEDATA_KEY] = language;
UrlHelper helper = new UrlHelper(viewContext.RequestContext);
return helper.Action(viewContext.RouteData.Values["action"].ToString(), viewContext.RouteData.Values["controller"].ToString(), viewContext.RouteData.DataTokens);
}