Using Razor to download file from html <a> attribute - c#

I am currently adding a notification for our customers. They should be able to download a pdf with list of our branches. When I click on the link nothing happens. I don't understand why. I'm using standard MVC approach. I tried to look at the path that is stored in the variable and the file exists there.
Anything I'm missing here?
Edit:
when opening the link in new tab chrome puts "about:blank#blocked" in url (popups enabled)
Thanks for any input
#{
var path = Server.MapPath(#"~/ToolsModule/Content/Forms/PdfTemplates/PBS/ListOfBranches.pdf");
}
<div>
<div>you can find a link to list of all branches <b>here</b></div>
</div>

Related

How do i load html from a db and display images that are stored locally in the project

I am storing the content for my page in a sql server database. When i load the content onto the page i have a tag:
<img src="~/Images/Course/html.jpg" />
The html is being rendered using #Html.Raw(content).
when the page loads i get the error:
GET http://localhost:51249/Course/Index/~/Images/Course/html.jpg 404 (Not Found)
I dont know why it does this. Is there any way to have images stored in files within the project, then when loading the html from the db the local linking works? Do I have to host the images somewhere and link to them via https? Any help would be great.
Fixed it - just remove the tilda from the value in the database. Here's a small sample code in the HTML to reproduce the same problem:
<img src="~/images/house.jpg" />
#{
var something = "<img src='/images/house.jpg' />";
}
#Html.Raw(something)
The first line displays the image direct from the location, using the tilda, which .net resolves to the root of your project.
The second line doesn't use the tilda, which automatically comes from the root of the project, but leaving the tilda in messes up.
Fix: delete one character from the HTML stored in the database.

Using Response.Redirect with Friendly URLS in ASP.NET

I am designing a website using friendly urls in ASP.NET. I am encountering an interesting bug when I am using response's redirect function with variables. When the page is redirected with a variable, the page you land on seems to think that the page you are on is in fact the home directory, destroying all links in the process. For example, if I redirect to a page and pass a variable like so:
Response.Redirect("nextpage/variable",false);
If I have an image on the next page, the link changes from:
<img src="images/foo.png">
to
<img src="nextpage/images/foo.png">
This happens no matter what the original path is. I have tried to link the image to the home directoty, ie:
<img src="./images/foo.png">
<img src="~/images/foo.png">
Nothing has worked. It now thinks the home directory begins at the new page (I assume because it sees the name of the friendly url and thinks its a folder)
To any of those looking for an answer, the trick is not to use './' or "~/", but simple to use the forward slash '/' , like so:
<img src="/images/foo.png">
The forward slash goes back to the root for all links and images. :)

Finding CSS selector path for Selenium C#

I am new to Selenium C# automation. Tried finding on web but did not get any help.
The html code looks like this. I need to find the element and then click it using CSS. The site only runs on IE.
<tbody>
<tr class="t-state-selected">
<td>Purchased</td>
<td class="">768990192</td>
I know web links can disappear, but here are a few I use when trying to figure out how to locate elements using Selenium's C# WebDriver:
https://automatetheplanet.com/selenium-webdriver-locators-cheat-sheet/
https://saucelabs.com/resources/articles/selenium-tips-css-selectors
https://www.packtpub.com/mapt/book/web_development/9781849515740/1
The bottom line is that you're selecting by id, class, or XPath. Each of these can be tested directly on the page using the F12 browser tools. For example, to find the first comment on your question above, you could try this in the console:
$x("//div[#id='mainbar']//tbody[#class='js-comments-list']/tr")
Here's another SO post with a quick and dirty answer.
And here is the official documentation from Selenium on how to locate UI elements.
To click on the number 768990192 which is dynamic we have to construct a CssSelector as follows :
driver.FindElement(By.CssSelector("tr.t-state-selected td:nth-of-type(2)")).Click();
You're really not giving us much info to work. I will try my best to accommodate. Even though the presented HTML is not enough to give an indication of the format and you've not presented any code of your current solution.
string url = "https://www.google.com";
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(url);
driver.FindElement(By.XPath("//tr[#class='t-state-selected']")).Click();
This little code snippet.
Creates a internet explorer driver.
Goes to the url of your choice.
And then clicks the table row that has a class that equals "t-state-selected'. Which my guess is all or none of the table rows.

Can I call an entry in a resource file using a method or variable?

I have several resource files built for a multilingual MVC 5 site I am working on.
I am using a CMS and the user can select the button text on certain content segments, like "Download" or "View More".
Rendering a value from my resource file using #Resources.Resource.ViewMore in my view works fine, but since the content is dynamic depending on what button text the user selects in the CMS, I need to use something like #Resources.Resource.#Model.getButtonText() but obviously that will not work.
Any suggestions on how to handle this?
You can use ResourceManager class for this
#{
ResourceManager rm = new ResourceManager(typeof(Resources.Resource));
#rm.GetString(Model.getButtonText());
}

Click an HTML link inside a WebBrowser Control

C# Visual Studio 2010
I am loading a complex html page into a webbrowser control. But, I don't have the ability to modify the webpage. I want to click a link on the page automatically from the windows form. But, the ID appears to be randomly generated each time the page is loaded (so I believe referencing the ID will not work).
This is the content of the a href link:
<a
id="u_lp_id_58547"
href="javascript:void(0)"
class="SGLeftPanelText" onclick="setStoreParams('cases;212', 212); window.leftpanel.onClick('cases_ss_733');return false; ">
My Assigned</a>
Is the anyway to click the link from C#?
Thanks!
UPDATE:
I feel like this is close but it is just not working:
HtmlElementCollection links = helpdeskWebBrowser.Document.Window.Frames["main_pending_events_frame"].Document.GetElementsByTagName("a");
MessageBox.Show(links.Count.ToString());
I have tried plugging in every single frame name and tried both "a" and "A" in the TagName field but just have not had any luck. I can just not find any links; the message box is always 0. What am I missing?
Something like this should work:
HtmlElement link = webBrowser.Document.GetElementByID("u_lp_id_58547")
link.InvokeMember("Click")
EDIT:
Since the IDs are generated randomly, another option may be to identify the links by their InnerText; along these lines.
HtmlElementCollection links = webBrowser.Document.GetElementsByTagName("A");
foreach (HtmlElement link in links)
{
if (link.InnerText.Equals("My Assigned"))
link.InvokeMember("Click");
}
UPDATE:
You can get the links within an IFrame using:
webBrowser.Document.Window.Frames["MyIFrame"].Document.GetElementsByTagName("A");
Perhaps you will have to isolate the link ID value using more of the surrounding HTML context as a "target" and then extract the new random ID.
In the past I have used the "HtmlAgilityPack" to easily parse "screen-scraped" HTML to isolate areas of interest within a page - this library seems to be easy to use and reliable.

Categories