ActionLink with simple URL - asp.net-mvc-4

My ActionLink reads as follows
#Html.ActionLink(image.FileName,image.FileLoc+"/"+image.FileName)
The image.FileLoc value is /content/3f1d6985-250d-45ff-abe6-dcab86437677
and image.FileName is mty2gm34.jpg
The corresponding href looks like this
<a href="/Images/content/3f1d6985-250d-45ff-abe6-dcab86437677/mty2gm34.jpg">
mty2gm34.jpg
</a>
Instead, what I would the href to look like is:
<a href="/content/3f1d6985-250d-45ff-abe6-dcab86437677/mty2gm34.jpg">
mty2gm34.jpg
</a>
Is there a simple way to do this?

Since you're linking a static resource instead of a controller action I'd forget about using HtmlHelper.ActionLink() and use UrlHelper.Content().
#image.FileName

Related

Razor Pages Tag Helper with Dynamic Parameters Assigning Current Url as Href

I'm running into trouble trying to set anchor tag helper parameters dynamically and looking for some help.
I have a nav that is a view component inside the shared _Layout.cshtml page that populates departments from a model.
#model List<DepartmentModel>
<ul class="nav">
#foreach (var d in Model)
{
<li>
<a asp-page="catalog/departments" asp-route-departmentName="#d.Name" asp-route-departmentId="#d.Id">#d.Name</a>
</li>
}
</ul>
Here is the InvokeAsync() from my View Component class
public async Task<IViewComponentResult> InvokeAsync()
{
var departments = _catalogService.GetNavDepartments();
return View(departments);
}
When I first launch the page, all the hrefs are populating correctly.
"catalog/departments/department-name-1/department-id-1"
"catalog/departments/department-name-2/department-id-2"
"catalog/departments/department-name-3/department-id-3"
If I click on one of the links, like the first link for example, I go to the proper department page "catalog/departments/department-name-1/department-id-1"
However, once I click that first link and navigate to the respective page, all the nav hrefs populate to the current url "catalog/departments/department-name-1/department-id-1" instead of the originally generated hrefs. This makes it so I can't navigate to another department.
Here is my route in the Startup.cs
services.AddRazorPages().AddRazorPagesOptions(options => {
options.Conventions.AddPageRoute("/Catalog/Departments", "{dName}/{dId}");
});
Based on the convention above, it eliminates the "catalog/department" piece of the url but I added it in this description for a sense of what I'm trying to accomplish. Even if I add this template to the page that populates the "catalog/departments" url, I get the same result.
#page "{dName}/{dId}"
Can anyone help me figure out what I am missing? Thanks in advance!
******** UPDATE ********
Currently, the only way I am able to get this to work is by adding the cache tag helper.
<cache>
<ul class="nav">
#foreach (var d in Model)
{
<li>
<a asp-page="catalog/departments" asp-route-departmentName="#d.Name" asp-route-departmentId="#d.Id">#d.Name</a>
</li>
}
</ul>
</cache>
This doesn't seem like a proper fix. Seems more of a hack then anything. Anybody have any ideas? Thanks!

Right way of using asp-page and asp-route in asp.net core razor pages between _layout and index page

Below is the file structure
I have default Index page under Pages folder, which has layout as _LayoutHarman.cshmtl
Code : Pages/Shared/_LayoutHarman.chtml
header menu : pages are in subfolder.ie category folder in this case
<a asp-route-cat_slug="electronic" asp-page="./category/Index">Electronic</a>
<a asp-route-cat_slug="beauty-care" asp-page="./category/index" >Beauty Care</a>
<a asp-route-cat_slug="toy" asp-page="./category/index" >Toy</a>
footer menu : pages are on root folder
<a asp-page="./Contact" >Contact</a>
<a asp-page="./terms" >Terms</a>
<a asp-page="./privacy" >Privacy</a>
Code : Pages/category/Index.cshtml
#page "{cat_slug?}/{pageIndex:int?}"
#model bList.Pages.category.IndexModel
#{
}
<nav aria-label="Page navigation example">
<ul class="pagination text-center">
#if(Model.prev_no!=0){
<li><a asp-page="category" asp-route-cat_slug="#Model.cat_url" asp-route-pageIndex="#Model.prev_no"> Previous</a></li>
}
#if (Model.no_more_record != "YES")
{
<li><a asp-page="category" asp-route-cat_slug="#Model.cat_url" asp-route-pageIndex="#Model.next_no">Next</a></li>
}
</ul>
</nav>
Here Next/ previous button generate url as follow
https://localhost:8080/category/toy/1
https://localhost:8080/category/toy/2
https://localhost:8080/category/toy/3
respective on selected category
Issue : When i visit on category page and click on prev or next button and then try to click on link Contact,Terms,Privacy i.e (which is on _LayoutHarman.cshtml) or on header menu then href become blank.
Edited One:
Code: On _LayoutHarman.cshtml
HeaderMenu:
<a href="./category/toy" >toy</a>
Footer Menu
<a asp-page="./Contact" >Contact</a>
<a asp-page="./terms" >Terms</a>
Code: On Category/Index.html page
Prev</li>
Next
Now on Next/ Prev button click , header menu generates url as
https://localhost:44382/category/toy/category/toy hence error page occurs. but for footer menu contact/term/privacy works properly
I did reproduce your problem, you need to delete the point(.) in asp-page attribute in your _LayoutHarman.cshmtl.
Changed like this:
<a asp-route-cat_slug="electronic" asp-page="/category/Index">Electronic</a>
<a asp-route-cat_slug="beauty-care" asp-page="/category/index" >Beauty Care</a>
<a asp-route-cat_slug="toy" asp-page="/category/index" >Toy</a>
<a asp-page="/Contact" >Contact</a>
<a asp-page="/terms" >Terms</a>
<a asp-page="/privacy" >Privacy</a>
And you don't need to add asp-page="category" in Pages/category/Index.cshtml, just delete this attribute in this page.
Here is my test result:
Do you really need all this extra server side processing, asp-route-cat_slug asp-page?
Can't you just use plain old fashion HTML hyperlink? As for the dynamic parameters maybe you can set only them...
View category
Just an idea to think on...
Cheers

Change a page using app-route and paper-icon-button

Based on the starter kit I have created a new app. I would like to link the different pages to a set of icons in the app-toolbar.
I got it working with:
<a href="/main-stream">
<paper-icon-button icon="icons:view-stream"></paper-icon-button>
</a>
But I think I am missing something here. Is there a better way?
in your app-Component you have a variable called page. This variable indicates the current view that is shown. To modify its value you can use the iron-selector like in the demo app.
<iron-selector selected="[[page]]" attr-for-selected="name" class="drawer-list" role="navigation">
<a name="view1" href="[[rootPath]]view1">View One</a>
<a name="view2" href="[[rootPath]]view2">View Two</a>
<a name="view3" href="[[rootPath]]view3">View Three</a>
</iron-selector>
Copied from:https://github.com/PolymerElements/polymer-starter-kit/blob/master/src/my-app.html

Vue js - how can I pass this property to a component in a loop?

I have a custom component that conditionally renders either a link to path or a span for disabled link if the supplied path-disabled method determines so, the internals of which are unimportant other than it works when used like this:
<li>
<conditional-link path="/step/1" :path-disabled="pathDisabled">
<span class="number">1</span>
Step one
</conditional-link>
</li>
But if I do this it fails:
<li v-for="route in stepPaths['/step'].subRoutes">
<conditional-link path="{{route.fullPath}}" :path-disabled="pathDisabled">
<span class="number">{{route.number}}</span>
{{route.title}}
this outputs correct path:
{{route.fullPath}}
</conditional-link>
</li>
The path property value is the litteral string {{route.fullPath}}.
I tried path="route.fullPath" but then the path is the litteral string route.fullPath.
How do I get the path value into the path property in a loop? The variable is correct as it renders fine within the inside of the component.
OK it was easy so in case any other newbies run into this, you have to bind the object in the v-for to be able to use the object directly:
<li v-for="route in stepPaths" :route="route">
<conditional-link :path="route.fullPath" ...

how to find the xpath for the below code

Trying to find the xpath for the below face book link .
<a class="home_fb_logo" target="_blank" title="facebook" href="http://www.facebook.com/chaturdn"></a>
<a class="home_tw_logo" target="_blank" title="twitter" href="http://www.twitter.com/chaturdn"></a>
<a class="home_sc_logo" target="_blank" title="soundcloud" href="http://www.soundcloud.com/chaturdn"></a>
<a class="home_go_logo" target="_blank" title="google+" href="https://www.google.com/+dnchaturvedi/about"></a>
<a class="home_yt_logo" target="_blank" title="youtube" href="http://www.youtube.com/chaturdn"></a>
<a class="home_li_logo" target="_blank" title="linkedin" href="http://www.linkedin.com/in/chaturdn"></a>
</div>
You can use this:
//a[#class='home_fb_logo']
use this //*[#title="facebook"]
to get the link find the element //*[#title="facebook"] and use getAttribute("href") to get the link in selenium.
To get a handle on the Facebook link WebElement you could use either of the following XPath expressions:
//a[#class='home_fb_logo']
//a[#title='facebook']
To get the actual URL from the link WebElement you can use the WebElement.getAttribute() method like so:
String url = element.getAttribute("href");