c# asp.net mvc HttpRequest.URL - c#

I have an ASP.net web application that has different functionality and data depending on the URL that it is logged in as.
Everything works fine in a deployed environment as I can bind several hosts in IIS and the web application can then interrogate the request.URL to work out which code and data to use.
eg. http://foo.bar.com will run different code than http://test.bar.com
The problem arises in debugging in VS2010 on my development machine.
I can hard code a default site url in the configuration but require several to be run at the same time.
e.g site1.localhost, site2.localhost, site3.localhost ....
I have tried editing my hosts file in system32/drivers/etc
e.g
127.0.0.1 localhost site1.localhost site2.localhost
but if I hit site1.localhost in my code the request.Url is always localhost
Is there anyway I can get around this?

If you have mapped your host name in system32/drivers/etc/hosts, try this code. This should return the correct path
string host = Request.Headers["Host"];
string pathAndQuery = Request.Url.PathAndQuery;
string fullPath = host + pathAndQuery;

Based on mfras3r comments. I can get it to work via
Setting up a website in IIS, point that web site to the directory of my MVC application.
Add the bindings for all the urls to the website in IIS.
Add the urls as entries in /etc/hosts pointing to localhost.
Build the application. ( Don't run it as it will screw up due to IIS)
Debug -> Attach to IIS process.

I think the debug server in vs2010 is not a full featured IIS. It do not support multiple site. So in your situation, it is better to use "attach" or remote debug. It is depends on whether you can install IIS on your development machine.

Related

Connection string in application settings doesn't work when https is enabled: Azure Web App

I have a .net core web app that works well in local environment. However, I got this exception when I publish it to Azure Web App:
Then I found this article and did the same approach as the image shows:
However, it fails once the setting is applied.
The difference between the tutorial and my web app is that my one has a SSL certification to enable https connection. Do I need some extra management to make the web app get the connection string under this condition?
EDIT:
I saw #lolops's post and found there was a right coonection string in kudu environment. So I published it again... and it worked. It's odd and I don't know why the problem has been gone.
What I did specially was only restarting my PC. Could this be a solution? Anyway sorry for troubling.
Try visiting https://YOURPROJECT.scm.azurewebsites.net and find "Environment variables", search for your connection string and compare to the variable you use. It will be prefixed with the type of connection you chose (SQLAzure).

Losing application Name from URL

I am using IIS to host an internal site. I have used visual studio express for web.
The problem I am having is when try to go to another part of the web site it drops the application name I have given it in IIS
Example:
www.name.com/applicationName/Home (this is whats working)
after trying to move to another part of the site the url becomes
www.name.com/Home/showDevice?sendmsg=192.168.0.1 (This does not work)
if I add the /applicationName/ to the above url it works correctly.
Is this and IIS issue I need to configure or is there something in the code I need to fix.

Deploy WebAPI to external website

We have a public website that is already exposed to the outside, although in reality there's really nothing there. Simply default.htm file with "Coming Soon" text in it. (http://vensuresoftware.com/)
We also have a WebAPI we've put together that we want to add to this website. When I publish locally to my IIS6, it works no problem. It's accessed as http://localhost/HRConnect/api/Claims just fine. I've used PostMan, a C# client, and Javascript AJAX to access this just fine. I can also load it in a browser at that URL, and I get the appropriate default controller and action.
However, I have been totally unable to accomplish this same thing on the website. Ideally I'd like to include it as a Virtual Directory to the http://vensuresoftware.com and access it as http://vensuresoftware.com/HRConnect/api/Claims but I've had zero luck doing so.
I have tried to add it as a Virtual Directory as well as an Application under that specific website, but when I access the URL, all I get is "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
I've ensure the Application pool is correct, with an appropriate user and the pass through connection test succeeds. But I just cannot access the service or the URL.
Any ideas or suggestions at all on what I can try? I'm not sure what else I can include here. Nothing special in IIS, nothing special in the service really. There's only 3 actions in it. As I said, it all works beautifully locally, under localhost though.
IIS 7 doesn't have built-in support for extensionless URLs which causes a lot of headaches trying to get MVC and Web API apps to run. I've gotten it to work using both these options. Pick the one that applies to you.
Install this IIS patch which allows IIS 7 to handle extensionless URLs.
If the patch isn't an option because you're worried about breaking other sites on the server, you can make the Web.config adjustment found in this answer. You'll have to do this for every MVC/Web API app you have running on the server.

WCF Service with Web Page not possible?

I have created a WCF Service and it's running just fine. I have the link that takes you to the Manager Service page in order for someone to get the URL for this service.
I have added a controller/cshtml page in order to test (ping) the databases that are used by the service so that we can check if they're up and running (the databases that is).
When I run the service locally with IIS Express, it works just fine.
www.domanin.com/ServiceName/Manager.svc is the URL.
When I run locally this ping (cshtml), it's localhost:XXXXX/Status.
Status is the name of the controller. And this works! (when I run locally).
Why does it not work when I publish it to a Server?
I try to go to www.domain.com/ServiceName/Status and I get a page cannot be found. I've also tried www.domain.com/Status and nothing. Can you not have a razor (cshtml + controller) hosted together with a service?
Thanks!
It sounds like your routing is not configured correctly most likely your mixing your wcf and mvc configurations and somehow it got messy. If you add .cshtml as an iis mime type on that remote server I suppose you could see a response.
I suggest that you create a folder under the root and make another iis application and place the test app and configuration in its own location if that is possible. You can still share code that way but will not have the overhead of opening the mvc pipeline for requests to your wcf service.

Human readable URL in azure

I have an azure cloud service with a couple worker roles, a WCF web role and a normal web role containing a couple aspx pages. Both roles are set to use https endpoints.
When testing locally in Visual studio using IIS the default.aspx page and the servicename.svc page both open in the browser. When I publish to azure though, the URL with the random string of characters opens the directory containing the service definition, but I can’t access the default.aspx page from that URL.
For example if my URL is randomstring.cloudapp.net after publishing, randomstring.cloudapp.net/wcfservice.svc works, but randomstring.cloudapp.net/default.aspx does not. I can get to the other page by using the IP address given in azure, but this isn’t really what I need. Furthermore, the cloud service URL does not work at all. mycloudservice.cloudapp.net results in webpage not found.
Do I need to do something special in order to get both the service, and the aspx pages to show up under mycloudservice.cloudapp.net? I can post the config files if need be, but as of right now, I don't think there relevant to the problem.
EDIT:
Just to clarify, I have now published to production, and the service is working [mycloudservice].cloudapp.net/[mywcfservice] and I can get to the website without a 404 error if I enter the port like [mycloudservice].cloudapp.net:8081/default.aspx . Is there a way to make everything show up under [mycloudservice].cloudapp.net/ without entering the port number so I can just type in [mycloudservice].cloudapp.net/[mywcfservice] AND [mycloudservice].cloudapp.net/default.aspx into the browser?
First, I guess you published your project to the stage slot of your cloud service. It's [Deployment ID].cloudapp.net. You will use the URL you specified if you deploy it to production slot.
Ref the problem, that '[randomstring].cloudapp.net/default.aspx' doesn't work, I'm not sure what you mean by 'doesn't work'. Is it 404 erroe? Or some asp.net runtime error. More information might be helpful.
Check the port numbers. Each of the endpoints (the wcf endpoint and the web endpoint) need to be on different port numbers. You'll need to access them as such when published to Azure.
The random-name issue is from publishing to Staging vs Production, as #Shaun pointed out.

Categories

Resources