IIS File Download without Extension - iis

I've got a .NET Web API 2 application, I've hooked up the api to send me a file id and from there I get the unique file from the server.
Example:
Download
I need it to be a unique id since there could be multiples of the file in the repo. However, when I try to click the download button I get a :
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I was thinking a re-write rule might be a good action, but I dont really want to rewrite it, i just want to allow anything /api/attachment no matter what the rest.
I've already got one rewrite rule since my page is a single-page-application to direct responses to the Default.cshtml like:
<rewrite>
<rules>
<rule name="Default" stopProcessing="true">
<match url="^(?!Lib|api|Assets|Views|Directives|Services|Controllers|signalr).*" />
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
</rewrite>
any thoughts on best way to achieve this?

I was able to resolve by creating an iframe and setting the src to the download like:
$("body").append('<iframe name="downloadFrame" id="download_iFrame" style="display:none;" src="" />');
and then in the C# I set the header like:
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

Related

rewrite a url to a soap method in iis

I am trying to rewrite a URL to a soap method but I seem no to able to figure out what's wrong. I have defined a pattern like this --> ./tree/tree/(.) with no conditions and no server variables in the action section I have both used redirect and rewrite to : localhost:2136/sharvand.asmx?op=GetTree but it hasn't worked out. any help will be appreciated
by the way how can I pass query string as a parameter to the web service
You can try this rule:
<rule name="test" stopProcessing="true">
<match url="^tree/tree/(.*)$" />
<action type="Rewrite" url="gis.shiraz.ir:80/service.asmx/op/{R:1}" />
</rule>

relative paths not functioning after site files uploaded to server

I developed a website locally using "localhost" and created all the site links, buttons, etc using a relative path structure. My server code is written in Node.js/Express. I am also using .ejs for a template engine...therefore all my server webpages are in a folder called "views/pages".
Using this structure, for example, I have a button with a link coded as:
Enter Site
On my development 'localhost' machine this was sufficient to call the '_landing' route in my server script which would then render the proper page to be served...without any difficulty. The server code would read something like this:
app.get('/', function (req, res) {
res.render('pages/_splash');
});
app.get('/_landing', function(req, res) {
res.render('pages/_landing', {user_stat: _subscriber});
});
Now I have uploaded my site files to a Windows server using Plesk software. The site files and folders are located in a directory named "httpdocs". It now seems my relative path links are broken. Using the button link above again as an example for some reason it now wants to route to "https://example.com/_landing"...which is obviously not correct. Why does it seem the relative path now seems to ignore the route call in Node.js...?
Any advice greatly appreciated, I am having extreme difficulty posting my site online due to a myriad of undocumented problems like this...posting a request to my server support will be useless since they do not respond to "coding issues". I thank you in advance.
For anybody that may be interested after several weeks I was able to determine the problem here. I eventually enlisted Plesk support in an effort to solve this issue. It was confirmed to me by a Plesk technician that my described issue was caused by a bug in the Plesk software platform.
The solution to resolve the bug was to insert some additional code in the 'web.config' file as follows:
<rewrite>
<rules>
<remove name="startup-file for site #1" />
<rule name="startup-file for site #1">
<match url="/*" />
<conditions />
<serverVariables />
<action type="Rewrite" url="/test2.js" />
</rule>
</rules>
</rewrite>
This code is to appear in the 'web.config' file AFTER "/httpErrors" AND WITHIN "/system.webServer"...the 'test2.js' should be set to the JS script file for your domain.

Modify requestvalidation in WebAPI (Azure)

We have a Asp.NET web api that handles requests from Android and iOS apps. We started to experience issues with GET requests that contained query strings. A URL like this: http://localhost:10723/api/Locations?userId=32432-a4r2-f32r3 gave this response:
A potentially dangerous Request.Path value was detected from the client (?)
After some debugging I saw that the querystring had been encoded, so the actual request was http://localhost:10723/api/Locations%3FuserId=32432-a4r2-f32r3, and that caused the issue. I can make changes to the apps that will fix this, but since this a app that is in production right now, I am desperately looking for a quick fix in the API that will allow the apps to work now.
What I have tried so far:
<httpRuntime targetFramework="4.5.1" requestValidationMode="2.0" />
<pages validateRequest="false" />
And related httpRuntime web.config tricks.
I have also written a custom request validator.
But everything is telling me that this is something that happens before the pagevalidation and my request validator is hit.
I was able to solve this by adding a rewrite rule for url on the server:
<system.webServer>
<rewrite>
<rules>
<rule name="Allowing querystrings.">
<match url="^(.*)\?(.*)$" />
<conditions logicalGrouping="MatchAny" />
<action type="Rewrite" url="{R:1}?{R:2}" />
</rule>
</rules>
</rewrite>
</system.webServer>

Match the content within a tag from a Non-HTML response in URL Rewrite

When going through a proxy server (A), any self-referential links sent from the apps server (B) need to be re-written to use the proxy as a host instead.
Here's an example:
Response from (B) contains: path
Proxy (A) needs to rewrite as: path
Normally, this is done by creating an outbound rule that inspects html responses for tags that contain urls, looks for references to to the apps server, and rewrites them.
Here's a normal rule GUI version:
<outboundRules>
<rule name="Outbound Links" preCondition="IsHTML" enabled="true">
<match filterByTags="A, Form, IFrame, Img, Input, Link, Script" pattern="(https?:\/\/proxy|^)\/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="http://apps/{R:2}" />
</rule>
Where IsHTML is defined as:
<preConditions>
<preCondition name="IsHTML">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="text\/html" />
</preCondition>
The problem is that some of the page content is returned via an XHR request. Minimally, this fails the HTML precondition.
but I can expand the rule to also include content types of xhr
However, URL Rewrite still has trouble parsing the returned text into tags because it is not valid HTML.
Here's an example of what the response looks like:
|6383|updatePanel|ctl00_mainContentPlaceHolder_contentUpdatePanel|
<div id="ctl00_mainContentPlaceHolder_resultsPanel">
path
</div>
...
|0|hiddenField|__EVENTTARGET||0|hiddenField|__EVENTARGUMENT||0|hiddenField|
However, when I do this, I get the error:
Sys.WebForms.PageRequestManagerParserErrorException:
The message received from the server could not be parsed.
You cannot modify XHR requests coming back from the ASP.NET. Doing so would be to attempt a Man In The Middle Attack (which your proxy is acting as), but Microsoft has good reason to prevent.
Here's a dummy message to explore the syntax ASP.NET uses in the response:
1|#||2|52|updatePanel|ctl00_mainContentPlaceHolder_firstUpdatePanel|
<p> New Content For First Update Panel </p>
The header starts with 1|#| | and then the number of updates in the rest of the message (2)
Then each update section follows the pattern:
|char_len|update_type|id_of_field_to_update|
New contents to insert into field
The len in each section must exactly equal the number of characters to follow. So finding and replacing content in these messages is extremely fickle.
The best recommendation is to simply return a relative URL that is server agnostic so the client can be redirected relative to their current domain.

IIS Rewrite 2.0 Module - 404 errors

I've installed the IIS Rewrite 2.0 module via Web Components on my Windows Server 2012.
I've read several articles but I just can't seem to get my simple rewrite to work.
I would like to rewrite http://www.acme.com/news/13/Jan/20 to http://www.acme.com/news.html#20-Jan-13
This is the rule I'm using:
<rule name="news articles" stopProcessing="true">
<match url="^news\/(.*)\/(.*)\/(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="news.html#{R:3}-{R:2}-{R:1}" appendQueryString="false" />
</rule>
When I apply http://www.acme.com/news/13/Jan/20 as my test pattern the rewrite works.
However, if I browse to http://www.acme.com/news/13/Jan/20 I get a 404 error:
Requested URL http://www.acme.com/news.html#20-Jan-13
Physical Path C:\Webs\acme.com\www\news.html#20-Jan-13
The physical file news.html exists and I can browse to it directly.
Is it the that are messing things up? Clearly C:\Webs\acme.com\www\news.html#20-Jan-13 isn't a physical file but I don't know how to solve this problem.
I can of course browse directly to http://www.acme.com/news.html#20-Jan-13 without issue.
Can anyone assist please?
Many thanks in advance.
Cheers,
Mark
According to this "How do write a rewrite rule in IIS that allows HTML anchors?" answer, browsers do not send anything after the # in a URL request to the server. They're markers intended solely for the browser to scroll to on the resulting page.
If you look at the logs, do you see a full request for the page including the anchor tag?
An alternative might be to use some DOM loaded JS to scroll the page to the anchor referenced in the URL (jQuery required):
$(function () {
var a_tag = $("a[name='" + window.location.hash.replace("#", "") +']");
$('html,body').animate({scrollTop: a_tag.offset().top}, 'slow');
});
Although, that likely doesn't solve the intended issue of friendlier URLs.

Resources