Showing posts with label ASP.NET. Show all posts
Showing posts with label ASP.NET. Show all posts

2024-10-13

ASP.NET Route results in 404 when the route ends with a filename

The Problem 

I had a custom route set on an API method that was to be used to delete a file from a ZIP created for an expense.

[HttpDelete]
[Route("api/reimbursements/expense/{expenseId}/receipt/{receiptFileName}")]
public async Task<IHttpActionResult> RemoveReceiptFromExpense(string expenseId, string receiptFileName) 
{
    // code to perform the remove here...
}

Everytime I tried to hit this endpoing using JavaScript's "fetch" API using the DELETE verb, it would always result in a 404. I spent more than an hour banging my head against the wall trying various different route changes, but could not understand why other endpoints on the same API controller worked except for this one.

The Reason

It finally dawned on me that ASP.NET has handlers that are filename-based, and they were intercepting my request and not finding a match that could fill it.  It wouldn't pass it to the routing handler that would go find my template and handle it.

The Solution

I don't have time today to research the more elegant solution, which would be to get the routing system to ignore the filename checks for this route and handle it with the filename at the end of the route, so I chose to reverse the template and put the receipt filename first and the expenseId last.  Now it looks like this:

[HttpDelete]
[Route("api/reimbursements/receipt/{receiptFileName}/expense/{expenseId}")]
public async Task<IHttpActionResult> RemoveReceiptFromExpense(string expenseId, string receiptFileName) 
{
    // code to perform remove here...
}

This made it work with no issues.  This is a quick solution for others experiencing the same issue.  

If you want to keep your route with the filename at the end, you will need to research how to force the "Extensionless" handlers to intercept your request for that template.  As I mentioned before, I don't have time today to research that, but needed to document this with the little time I have, so I can remember why next time I run into this issue.


2020-02-19

Access ASP.NET website hosted in IIS Express from another machine on the local network (VS 2019)

I needed to test Mac Safari against a website I was developing on a Windows machine. Instead of being required to publish for every change, I wanted to just expose IIS Express across the LAN and hit the website that way. In order to do so, I had to do the following:

  1. Run Visual Studio 2019 as Administrator
  2. Edit {ProjectRoot}/.vs/{WebProjectName}/config/applicationhost.config
  3. In the <sites> section, add a binding under the website project's <site> definition (see below):
  4. Allow the desired port through your firewall
<site name="FocusAndExecute" id="1">
  <application path="/" applicationPool="Clr4IntegratedAppPool">
    <virtualDirectory path="/" physicalPath="{path_to_site}" />
  </application>
  <bindings>
    <binding protocol="http" bindingInformation="*:51427:localhost" />
    <binding protocol="http" bindingInformation="*:51428:192.168.1.123" />
  </bindings>
</site>


After doing this and launching IIS Express from Visual Studio, I was able to hit the site from the Mac by going to:
http://192.168.1.123:51428

NOTE: You may be able to put *:51428:* instead of specifying the IP, so you can hit it by host name, etc. but I didn't play with that yet.  Feel free to leave a comment if you have a better way!

2010-08-26

Making sure JavaScript gets called after an AJAX Partial Postback in ASP.NET

ScriptManager.RegisterStartupScript(Page page, Type type, string key, string script, bool addScriptTags)

Notice the first parameter of this method, as it has an overload that takes a Control as its first parameter.  When you use the Control overload, it only registers the startup script once on the initial rendering of the page.

If you use the Page overload listed above, it will register it for every asynchronous postback to ensure your JavaScript is executed everytime.

Remember to use ScriptManager and not ClientScriptManager...

From the Remarks on the MSDN article for the overload listed above:
"You use this method to register a startup script block that is included every time that an asynchronous postback occurs. To register a script block for a control that is inside an UpdatePanel control so that the script block is registered only when the UpdatePanel control is updated, use the RegisterStartupScript(Control, Type, String, String, Boolean) overload of this method.

If you want to register a startup script that does not pertain to partial-page updates, and if you want to register the script only one time during initial page rendering, use the RegisterStartupScript method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page."

Also very important:
Make sure you have the following using statement in your codebehind...
using System.Web.UI;

If you don't, you will get the error:
Cannot access internal property 'ScriptManager' here

2009-06-29

Ensure XHTML Strict Compliance by Removing Name Attribute from Form tag in ASP.NET

Many of us web developers are trying to be "good citizens" and conform to the higher standards of XHTML in our apps.

By default, ASP.NET will add the name="FormName" attribute to the rendered XHTML.

To force ASP.NET to omit the name attribute, simply add the following tag to the system.web section of your web.config file for your app:

<xhtmlConformance mode="Strict" />


Be warned, however; unfortunately it doesn't seem to make ASP.NET render HyperLinks properly. I have an asp:HyperLink that happens to have an ampersand (&) in the Text and ToolTip properties. The ToolTip properly renders a title="" attribute with the ampersand encoded as &amp; ... but the text between the open and close <a> tags doesn't have the ampersand properly encoded.

The only advice I can give you there is to:
1. Use the word "and" instead of an ampersand
2. Make static "a" tags instead of using the ASP.NET HyperLink object, if you can. If someone knows a smoother/quicker/more elegant solution, please leave a comment with it!

2009-04-29

"The breakpoint will not currently be hit" "The source code is different then the original version"

Today, I was debugging an ASP.NET website. I was at a breakpoint when a co-worker called me over to their desk to help with a defect they were trying to reproduce.

I needed to use remote desktop to look at something on my own machine, so I did so. While I was accessing it via remote desktop, my Visual Studio decided to crash.

Aftwerward I started seeing the following on breakpoints I was trying to hit:
"The breakpoint will not currently be hit. The source code is different then the original version"

I thought that a reboot would help, but to no avail.

After talking to another co-worker that had seen it in the past, he informed me that he fixed it by deleting all of the Temporary ASP.NET Files.

Sure enough, it worked like a champ. However, I had to shut down anything that had handles on those files (like the ASP.NET development server, Visual Studio, etc.) before I could complete the delete.

So to fix the problem:
  1. Close out Visual Studio and make sure any instances of ASP.NET development server are closed as well.
  2. Delete everything from "C:\Windows\Microsoft.NET\Framework\v.2.0.50727\Temporary ASP.NET Files" (where v.2.0.50727 is the version of the .NET Framework your site is running on.)

Hope this helps someone else... it sure helped me.

2009-03-04

Error connecting to undo manager of source file "whatever control/page.designer.cs"

I've run into this problem a couple of times where I get an error that states:
Error connecting to undo manager of source file "MyControl.ascx.designer.cs" (I've seen it with an aspx.designer.cs too.)

I don't know what the cause is, but I have a sneaking suspicion that the culprit in both of my cases was merge operations from source control interactions where, for whatever reason, my designer became out-of-sync or something with its ascx/aspx counterpart.

After searching around the net, it seemed the most popular method of correcting the problem was to delete the designer completely, then run the option to convert the project to a Web Application.

That option didn't sit right with me. I searched on.

I found an article by Scott Hanselman that discussed excluding the designer from the project, recompile, re-include, recompile again. That seemed a much more fitting solution to me; but I wondered if it could be even easier. So I tried an experiment.

My experiment consisted of simply opening the designer.cs file, typing a character somewhere in the file, deleting the new character, saving the file and recompiling.

The problem disappeared for me after that. (I will note that I cleaned the solution and rebuilt it prior to trying this, and that may be a required piece of the solution to the problem, but I would try the simple step first, then add in the clean/rebuild after.)

I'm using Visual Studio 2008 SP1. I hope this helps someone else out there.

2008-12-18

ASP.NET Development Server Responds with 403 - Forbidden in Firefox

I've been working on an ASP.NET site for a few months that requires Windows Authentication, and it was working fine when running the site in the ASP.NET Development Server and accessing in Firefox.

That is, until today.

Today, it decided to start kicking back Forbidden messages every time I tried to access it. Why would it start doing that to me?

It turns out I had at some point told Firefox to remember the password for the site, then had to change my domain credentials after doing so. Since my saved password didn't match the new password, the site was forced to return a Forbidden. I would hope that Firefox would prompt me for a new password when the authentication failed, but it didn't.

So how do we get back to good in Firefox?
You have to go into the Saved Passwords settings in Firefox and remove the site from the list of passwords to store.

In Firefox 3:
Tools | Options
Click on the "Security" button on the ribbon at the top of the Options dialog
Firefox Security Options dialog
Click the "Saved Passwords..." button in the "Passwords" section of the "Security" tab.
Find the offending site in the "Saved Passwords" dialog and click the "Remove" button.
Firefox Saved Passwords dialog
Click "Close" on the "Saved Passwords" dialog.
Click "OK" on the Options dialog.
Open the site again and Firefox should prompt you for the new credentials.

Hope this helps!

2008-10-18

ASP.NET - Getting the Virtual Application Root Path for Your Site

HttpContext.Current.Request.ApplicationPath

I had to blog this here, because I have the need for it every once in a while, but not frequent enough to remember it... :)

Examples:
A site hosted without a virtual directory: mysite.com/
Returns: "/"

A site hosted from a virtual directory: mysite.com/myVirtualDir
Returns: "/myVirtualDir"

See the MSDN Documentation for more details and code examples.
See the "ASP.NET Web Site Paths" article for more ways to discover paths.