I am using Thymeleaf Template Engine with Spring Web MVC and I am got stuck while creating url's with the help of current url. Is there any way to get current inside Thymeleaf HTML file? eg: Suppose my current url in my browser address bar is:
http://localhost:8080/project/web/category/mobiles
and now I want to make a url like this http://localhost:8080/project/web/category/mobiles/store/samsung
or
http://localhost:8080/project/web/category/mobiles?min_price=10&max_price=100.
So I the code will look like this
<a th:with="currentUrl='http://localhost:8080/project/web/category/mobiles'"
th:href="#{__${currentUrl}__/__${store.name}__}">
Click to More Result
</a>
Here I am using currentUrl variable with hardcoded url, So I want to some solution for the same. The hardcoded value will not work everytime because I have dynamic categories.
I tried the same with relative url but its not working for me.
<a th:href="#{/store/__${store.name}__}">Click to More</a>
//will produce: http://localhost:8080/project/web/store/samsung
//I want: http://localhost:8080/project/web/category/mobiles/store/samsung
Please have a look and let me know if am I doing something wrong.
Oh I got the solution for this. I missed the {#httpServletRequest.requestURI} in the documentation.
Here is the solution which is working for me:
<a th:href="#{__${#httpServletRequest.requestURI}__/store/__${store.name}__}">Click to More</a>
//Will produce: http://localhost:8080/project/web/category/mobiles/store/samsung
You can get the current URL using two single ' quotes. Example:
<a th:href="#{''(lang=de)}">Deutsch</a>
Note that this unfortulately does not include existing URL parameters.
In Thymeleaf 3.1, the current URL can be obtained from the context object:
<form th:action="${#ctx.springRequestContext.requestUri}">
If the requested url is for example : http://localhost:8080/my-page
I could get this value: /my-page using this: httpServletRequest.requestURI
For example, if you have to load some resource , you can use:
<script th:src="#{|${#httpServletRequest.requestURI}/main.js|}"></script>
And renderized html will be:
<script src="/my-page/main.js"></script>
Related
I'm using anchor tags helpers, which work fine for the most part. I'd like to clean up a particular URL that is getting generated, My tag looks like this (note that type is more specific than id in this case, not that it matters):
<a asp-controller="SomeController" asp-action="SomeAction" asp-route-id="#Model.ID" asp-route-type="#someType">
I would like to get a URL that looks like this:
http://localhost:1234/SomeController/SomeAction/42/1
But instead I get
http://localhost:1234/SomeController/SomeAction/42?type=1
Is there any way to get the 'additional' parameter(s) in a RESTful format, instead of a GET string? I could always just modify the route or URL in a more conventional manner, but was curious if I could make this work.
I'll answer my own question in the hopes that it benefits someone else. The issue was that I needed to create a custom route, like the below:
routes.MapRoute(
name: "newRoute",
template: "{controller=SomeController}/{action=SomeAction}/{id}/{type}");
Interested in alternative approaches, if there is a better practice.
I've been having issues with this but I think is simple.
asp.net core 2.0 Razor Pages
I have a Users/index page and I added a link to take me to the child records under Entries Folder.
Structure is like
Pages
/Users
/Entries
Under /Pages/Users/index.cshtml
<a asp-page="./Edit" asp-route-id="#item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="#item.Id">Details</a>|
<a asp-page="~/Pages/Entries/" asp-route-id="#item.Id">Enter Child Records</a>
However, on the browser, looks like is not rendering the correct link. It staying under the default page.
I tried asp-page="../Entries/" and other combinations with no luck.
Can't believe this but this works.
<a asp-page="../Entries/Index" asp-route-id="#item.Id">test</a>
I think your own answer might add confusions to others. First of all, you're talking about LINKs to another page, not redirect to another page. Razor Pages have a designated function for page redirections.
About the asp-page tag helper, it's interpreted by the code-behind engine (e.g. C# syntax) and so the "~" doesn't work. Basically, asp-page is expecting a name. In your own examples:
<a asp-page="./Edit" asp-route-id="#item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="#item.Id">Details</a>|
will work, but they should be shortened as:
<a asp-page="Edit" ...>Edit</a> |
<a asp-page="Details" ...>Details</a>|
because they are in the same folder as your /Users/Index page. As you know the "./" part just means going up one level to your parent folder and so it's redundant. You don't want to instruct your engine to go up one level and then come down again to the same folder immediately.
When you need to switch to another containing folder, going up two levels and specify the new folder name. So, the "../NewFolder" works.
If it is at the web site root level, you can use the root syntax directly for faster routing. So both of these work the same if AnotherFolder is under root:
<a asp-page="../AnotherFolder/Index" ...>test</a>
<a asp-page="/AnotherFolder/Index" ...>test</a>
The asp-page tag helper is expecting a real page name at the last part and hence the quoted text must be ended with a page name. You cannot use asp-page="/Entries/" as in HTML URLs and expect the engine to default to Index page for you.
Lastly, to cover redirecting pages, code-behind has a function called return RedirectToPage(string pageName, ...), and its first parameter uses the same page routing syntax as the asp-page tag helper described above.
There is also a Redirect() function that works with pages outside your Razor Pages system, which can take any URLs as your browser or HTML code do. That's another story.
I am using Codeigniter independently first time for my personal project for practice.
I have created controller class and some other methods in that also created custom url using route.php i.e. as follwed:
$route['cele-saree'] = "celeSaree_c";
("celeSaree_c" is my controller class index and I am making url as cele-saree - and its working properly)
Then I made some other methods in that class controller as follows:
public function pg1() {
//page and data load here
}
Then goes to route and created url for that with following code:
$route['Priyamani-saree-images'] = "celeSaree_c/pg1";
(This code gave me url "localhost/mysite/Priyamani-saree-images" - Which working fine as I want)
But I want url as "localhost/mysite/cele-saree/Priyamani-saree-images" and for that I used following code:
$route['cele-saree/Priyamani-saree-images'] = "celeSaree_c/pg1";
(When I goes to "localhost/mysite/cele-saree/Priyamani-saree-images" I got all content properly but found that
its stylesheet source is changed:
"localhost/mysite/assets/cele_saree/css/cele_saree.css"
to
"localhost/mysite/cele-saree/assets/cele_saree/css/cele_saree.css"
which causing error 404.
Same thing is happend with internal images also.)
I have set base_url:
$config['base_url'] = 'localhost/mysite/';
Please guide me what to do regarding it.
In my opinions, for image/css/javascript I preferred to get the resources by calling this way:
"/assets/cele_saree/css/cele_saree.css"
^
|
This slash will call from root path
instead of using base_url() like
"localhost/mysite/assets/cele_saree/css/cele_saree.css"
Example:
<link href="/assets/cele_saree/css/cele_saree.css" rel="stylesheet" />
<img src="/assets/cele_saree/images/sample_image.png" />
For your case, if you want to change the routes based on your
convenience it will not effect the source path.
In ASP having this URL:
http://www.example.com?foo=1&bar=2
Request.QueryString["bar"] returns NULL
The URL is a map area "href" link which I have assigned like so:
PolygonHotSpot p = new PolygonHotSpot();
p.NavigateUrl = http://www.example.com?foo=1&bar=2
ASP automatically HTML encodes the URL for the href, but it is not HTML decoding it again in the request therefore query string "bar" is not found.
Now I am using IIS URL Rewrite 2 module. Maybe this module is causing the problem? What can I do to solve it? I have tried using URL rewrite rules but couldn't figure our how or if it is the proper way.
It's probably not a good idea, but you could use Request.ServerVariables("QUERY_STRING") (or Request.ServerVariables["QUERY_STRING"] - your tags say ASP classic but your code looks like C#?) to get at the entire thing and then process it yourself.
I think there must be something deeper wrong though. A link can be encoded to be sent to the browser - the browser does the work of decoding it before navigating to the link. You can demonstrate this with a simple <a href="/test?a=1&b=2"> in a test script - the browser ends up correctly at /test?a=1&b=2. Testing it with a polygonal image map shows the same behaviour.
If you can show me what is in your actual HTML output for the image map I might be able to help more.
Favicon is displayed fine if the URL have no querystring, while it does't show if the URL contain querystring!!
Here are the page without querystring:
www.NoonOffer.com
Here are the page with querystring
http://www.noonoffer.com/Offers.aspx/7/Pay-LE-50-and-get-50-discount-on-PMP-course
Any suggestions?
Your url doesn't appear to be correct. Something in your code is not putting the proper URL in the HREF.
It is:
http://www.noonoffer.com/Offers.aspx/7/favicon.png
Should be:
http://www.noonoffer.com/favicon.png
EDIT: Not sure with .NET, but using Core Tag Library with Java Spring MVC a "/" at the front of the URL will make a difference when using relative paths.
This would append it to the end of the URI I believe:
<c:url value="/favicon.png">
And I believe this would append it to the context root like you want.
<c:url value="favicon.png">
This may not be the exact issue, but I hope it will help.