CSharp Adding my site url path to the front of a Hyperlink - c#

I am having an issue. I have page urls coming in as strings from my DB via csharp access identifier.
Example of cshtml line:
#data.PageTitle
When I try to click on the link the link appears as:
https://www.MyAPP.com/dmr/home/www.LINKURL.com
How to I get the clickable anchor tag to only display
www.LINKURL.com
Any ideas or guidance would be appreciated. Thank you.

Nothing to do with Razor or C#.
The browser doesn't know that it's a domain - it is interpreting it as a file path. Use a protocol http://, https:// or // (this means use the same protocol as the current page).
You might consider adding data validation before saving potentially invalid URLs in your database.

Related

c# mvc why are urls combining when requested?

This is my first time developing something in C# and in MVC. I deployed my application and everything seems to be functioning ok except for the navigation. The first thing you click on will direct you properly, but the second time will try to combine the pathing request with the first.
Ex. Clicking on Services drop down and click ProcessReports correctly returns the web page, with the URL:
http://{ServerName}/SupportPortal/Support/ProcessReports
Then, clicking on a dropdown and then DalimWebApp Dev returns a 404 error and the url is
http://{ServerName}/SupportPortal/Support/Support/DalimWebApp/Dev
Why are there two "Supports?"
Any ideas?
How are you generating the URL for your links?
If you're using an HTML anchor with an href value "Support/ProcessReports" instead of "/Support/ProcessReports" (mind the forward slash) the URLs will be relative to your current path. Try prepending the forward slash.
As #David mentioned, you could also use the built in helpers to generate the entire link for you if it's easier for you.

How to add an download link to users in my project

In my project i will be having an link like
Download
I want the users to download files of different types. The file will be in the root folder. When i am clicking on the link it is displaying an error. This is the plugin to install in the chrome. If the user download this link and open then it will automatically add to the chrome.
How can i do this.
The file is not even downloading.
This isn't a valid path:
~/hello world.crx
The ~ character is for use server-side to denote the root of the application. Client-side it has no meaning. The browser doesn't know what the root of the application is (or what the application is at all), it's just sending requests to resources at addresses. And it doesn't know what to do with that address.
You'll need to either use some server-side logic to translate that path into a browser-useable path, or manually make it a relative or absolute path.
If the ASP.NET MVC Framework isn't translating this for you then you're probably using a version that requires a little more manual work for it. Try something like:
Download
(Note: This assumes the use of the Razor view engine. If you're not using that then you'll want to use whatever your view engine equivalent is.)
What you need to do is set up a directory online, where you can host the file.
I also see that in your aref you don't want to type the full path so denote it with a /hello_world.crx, but make sure that you've set up a base href:
<base href="http://yourdomain.com/something/">
Try renaming the file to remove any spaces e.g. "hello_world.crx" and then change the name in the link code to match.
if a webpage and the downloadable file is in the same location
(i.e)
SampleFolder->Download.html
SampleFolder->hello world.crx
then try the below
download
If the webpage and the downloadable file in different location
(i.e)
SampleFolder->Download.html
SampleFolder->Downloads->hello world.crx
then try the below
download

Redirecting url to index.aspx page using standard asp.net3.5 and web.config

I have a subdomain that is http://trade.businessbazaar.in . I am dynamically creating urls from database something in this manner http://trade.businessbazaar.in/mycompany. To display details, I have an index.aspx file there,thinking that on every request the index.aspx page will load and display data accodingly. Also, There is a masterpage on the index.aspx page from where i am capturing the text mycompany and query it in database to fetch result. But nothing seems to work.
A genuine link is http://trade.businessbazaar.in/Symparlife. But its unable to load index.aspx. I need a clean approach without any third party dll or rewriters. Directly to push some lines in config and start working. That is url will be the same but index page will get loaded...
In short, i want to say
I need the StackOverflow type clean url mechanism to fetch pages
Thanks in Advance
You can handle the Begin_Request event in Global.asax and add custom code to redirect to index.aspx and convert the parts of the URL into query string arguments. You should use Server.Transfer to keep the URL in the browser.
I'd recommend upgrading to 4.0 and using the Routing enine though. You should check if the standard routing is available as a download for ASP.NET 3.5. I am sure your code will get messy very soon. Been there, done that.
As #Mike Miller mentions in the comments the Routing engine ships with ASP.NET 3.5. You can check the documentation here - http://msdn.microsoft.com/en-us/library/system.web.routing(v=vs.90).aspx
Here is a tutorial on how to use it with Web Forms - http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
For your case the code would be something like:
routes.MapPageRoute("company-index", "/{company}", "~/index.aspx")
And in index.aspx you can access the route value for company like this:
string company = (string)Page.RouteData.Values["company"];
Keep in mind that you'd better add something in the URL before your actual argument (the company name). If you don't you will have problems later on when because you may want to add a URL like "/Login" but then you will have to validate that users can't create a company named "Login". Not how Stack Overflow has "/questions/" before the actual question info in the URL.

Hiding default.aspx from the URL

I wanted to know if there is a solution using IIS6 for an application to get rid of the default.aspx text in the url. so for example if a user hits:
www.website.com/default.aspx
the browser only shows:
www.website.com/
No matter what.
It's just for SEO.
I already use UrlRewriting.NET for some rewrites in my app but for I'm not that clever to create a rule for that.
Any help is much appreciate.
Thanks.
Jose
I think ScottGu already has the topic of rewriting in ASP.NET covered: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx.
He covers things such as:
Rewriting using UrlRewriter.net, ISAPI Rewrite
ASP.NET Tricks, posting back (hitting the friendly version of the url)
With your problem I think you need to use a combination of, never linking to 'default.aspx' ie. ONLY link to '/'. Then use Scott's Form Postback browser file to ensure postbacks always hit that same friendly version of the url.
Redirecting 'default.aspx' to '/', which then gets served by 'default.aspx' sounds like a recipe for disaster to me. Just fix your links to ensure you never end up on 'default.aspx' explicitly.
I think the simplest way to change the search results index (assuming it knows about HTTP 301) is to write a little function in your default.aspx's Page_Load that redirects the browser using a 301 Moved Permanently (or 302 Moved Temporarily).
void Page_Load(...) {
if(Request.Path.EndsWith("default.aspx", true/*case-insensitive*/, null)) {
Response.StatusCode = 301;
Response.StatusDescription = "Moved Permanently";
Response.Headers.Add("Location", "/");
HttpContext.Current.ApplicationInstance.CompleteRequest(); // end the request
}
// do normal stuff here
}
If you have something to do URL rewriting, then all you need to do its ensure that your links point to the correct URL.
If you don't fix your links, its up to the browser to decide if it wants to display the actual link it requested.
If you would really like to do a dodgy job, you can use some javascript to make the address bar of the browser display whatever you like, even if its invalid.
If default.aspx is set as the default document to serve in IIS, and all your internal site links contain URL's without the default.aspx then I think that should do the trick.
Although the user can still type in default.aspx, search engine spiders should just pick up the friendlier URL's from your link href attributes.
The way I would do it is to use Application_BeginRequest in public class Global : System.Web.HttpApplication and check the HttpContext.Current.Request.URL for default.aspx, and then use HttpContext.Current.Response.Redirect from there if you find it.
The downside is having a redirect is not always so great and this isn't going to work if you are posting data to that default.aspx page. But you can't simply trick the browser into showing a different url, though you can tell ASP.NET to serve whatever page you want for any request.

Trailing slash in URL causing Partial Post Back issues

.NET 3.5 app written in C# here with both jQuery and some ASP.NET AJAX UpdatePanel flavouring.
I'm running into an interesting issue. I created a pagination user control that is made up of LinkButtons. The user control fires off an event called CurrentPageChanged whenever someone clicks on a page, previous, first, or next buttons. The page using this pagination control is then responsible for getting the newest set of records based on the pagination control item clicked.
Now, the issue I'm running into is this:
If I have a url like this: http://localhost:2798/user/9794/profile, everything works fine. However, if I have a url with the trailing slash (i.e. http://localhost:2798/user/9794/profile/), my UpdatePanel falls on its face with a 405 error.
The exception in question is this:
[Exception] Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 405
Now, I looked at the requests through Chrome's Developer Tools, and I see that it's requesting this url:
http://localhost:2798/user/9794/profile/profile. It looks like if there's a trailing slash, it'll append an extra path.
Any ideas how I can get around this?
It looks like if there's a trailing slash, it'll append an extra path.
Yes, that's how relative URLs work. A browser uses the trailing slash to decide whether the URL refers to a folder (in which case a URL could refer to another file inside that folder), or a file (in which case it would have to look up to the parent folder). So <a href="profile"> inside the trailing-slash-URL will indeed be pointing at .../profile/profile.
Any ideas how I can get around this?
Use absolute URLs (or, better, root-relative URLs like href="/user/9794/profile" everywhere you make a link (either explicitly or via an ASP.NET control). Relative URLs are incompatible with the ‘routed’ style of URL where you may have a variable number of slash-separated bits of data in the URL.
And/or use only canonical URLs, so that the URL for any given resource is always fixed; if you go to the “wrong” version with an extra slash or other redundant stuff in the URL you get a 301 redirect to the “right” version.

Categories

Resources