c# mvc why are urls combining when requested? - c#

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.

Related

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

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.

Clicking on link removes the application name from URL, and moves back

I have a Website www.xyz.com hosted on IIS.
I have created few child applications within same website.
So, my url becomes www.xyz.com/app1, now on aap1 home page I have below link, which opens the page on same app1
Index
But when I click on the link, It removes application name (i.e., app1) from URL and browse like below
www.xyz.com/Masters/Index and I end up with resource not found error.
Whereas it should be www.xyz.com/app1/Masters/Index
I tried link with ../, /, ./ prefix for link but none of them seems to work. I'm a missing something?
Please help.
From within your app1 use an Html.ActionLink helper instead fo the hardcoded link:
#Html.ActionLink("Index", "Masters", "Index")
This should keep the existing routing of the current web application.
LinkExtensions.ActionLink Method (HtmlHelper, String, String, String)
You can also use #Url.Action instead if you prefer to just update the url within your mark-up:
Index

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.

POSTing to a re-written URL on IIS 6 doesn't work

I am working on a site which is programmed in C# .net. It uses a CMS called ADX Studio (a decision which predates my time there) which provides a shonky form of URL Rewriting (as far as I can tell it works by assigning an aspx page as the default 404 handler in IIS).
I have an web form which lives at a rewritten URL. I edited it so that the html form's action points back to the rewritten URL:
var u = new Uri(Request.RawUrl.Split(new char[1] { ';' }).Last());
userAdminForm.Action = u.PathAndQuery;
(kind of ugly but works based on what Request.RawUrl is on these rewritten URLs).
The "pretty" URL is something like this:
http://www.site.com/admin/user/edit/
On my development box (Windows XP/ IIS 5) when I initially tried POSTing back to URLs like this I got a HTTP 405 error. I worked around this by adding a script mapping so Aspnet_isapi.dll handles all (*) requests. And everything works fine on my development machine.
I just pushed my changes to the live server (Windows Server 2003 R2 and IIS 6) and the post fails silently. The page refreshes but all of my logic (from within an IsPostBack path in the code) doesn't get hit. No errors are displayed, it just doesn't work.
If I remove my code setting the .Action of the form then the postback works but it is posting to the ugly URL corresponding to the physical location of the aspx file rather than my page.
Am I missing a simple way to make this work? I don't want to be switching URL rewriting method or anything as this is a large legacy site and is unfortunately pretty dependent on ADX Studio so I don't want to do anything that will break that.
[edited because somehow the code above lost its code highlighting]
The issue is that the page's <form> tag is referencing the "ugly" url as the action. You can resolve that by completely removing the action tag from the form. Browsers will, by default, postback to the same page, ie. the "pretty" url.
This article explains how to accomplish an "actionless" form (~ two thirds of the way down) http://msdn.microsoft.com/en-us/library/ms972974.aspx
It seems like the problem is the same as it was on IIS 5. I can get it to work by doing the following in the IIS Manager:
Right click on the relevant website and select "Properties"
Choose the "Home Directory" tab
Click "Configuration" down in the "Application settings"
Click "Insert" next to the "Wildcard application maps"
Browse to the location of aspnet_isapi.dll (in my case: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll )
Untick "Check that file exists"
Click "OK" back through the Russian doll of dialogs.
This is basically the same as the approach that I linked to in the question for IIS5. However, it's not optimal because IIS is running every request through asp (even static files). Which seems like it can only slow things down. I'd like to be able to specify that asp only needs invoking for HTTP POST requests at least.
The weird thing is that IIS5 gave a HTTP 405 error when POSTing to an extension without a registered ISAPI extension but IIS6 just fails silently. And the page is being run through IIS (I can debug with a breakpoint in the Page_Load function) but IsPostBack (and IsCrossPagePostBack) don't get correctly set. Could it be related to the view state? Is there any alternative to my solution described above?
I've come to what I think is an optimal solution for this problem. It turns out that ADXStudio CMS does use the default 404 rule to do some form of URL rewriting. This has a problem with http POST:
when IIS initially executes a custom
URL on a 404 error, it changes POST to
GET, even if the client does a POST
request.
(thanks to elite brains' blog post about setting up IIS6 and ASP.NET MVC).
Rather than creating my own HttpModule I decided instead to use Ionics Isapi Rewrite Filter to rewrite my URLs. I then set the 404 error handler in IIS to the default. And I created this IIRF.ini file to redirect all requests to the same format as the 404 handler produced:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /Default.aspx?404;http://%{HTTP_HOST}$1 [U,L]
And everything seems to work great. The advantage over my previous answer is that the rewrite code is low level and runs fast and the -f and -d switches mean that if a file actually exists it isn't re-written and so static files don't have the overhead of running through .net.

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