如何重定向至DLL文件中的Aspx页面?该操作是否可行?
I've created a DLL where I added an ASPX file in a views folder at the root of the DLL. I want to call a method in the DLL's class and redirect to that specific ASPX page. Here's the basic implementation code in my DLL:
public void redirect(Dictionary<string, object> obj){ try{ Console.WriteLine("data received"+obj); string encryptedValue = obj["data"].ToString(); string endpoint = obj["endpoint"].ToString(); if(!String.IsNullOrEmpty(encryptedValue)){ Console.WriteLine("Redirecting for Payment to : "+endpoint); RedirectionProperty property = new RedirectionProperty(endpoint,encryptedValue); Console.WriteLine(property.Url); HttpContext.Current.Session.Add("url",property.Url); HttpContext.Current.Session.Add("data",property.PaymentData); HttpContext.Current.Response.Write("hello"); HttpContext.Current.Response.Redirect("/views/Redirect.aspx",false); } }catch(Exception e){ Console.WriteLine(e.Message); throw new ApplicationException("Error performing Redirection "); } }
Is it possible to redirect to the ASPX page that exists inside the DLL?
Short answer: No, you can't directly redirect to an ASPX page embedded inside a DLL using Response.Redirect like you're trying to do. Here's why, plus practical workarounds to achieve your goal:
Why your current approach fails
ASP.NET relies on the web server (like IIS) to serve files from the web application's physical directory structure. A DLL is a compiled binary—its internal files (like your views/Redirect.aspx) aren't exposed as accessible web resources by default. When you call Response.Redirect("/views/Redirect.aspx"), the web server looks for that path relative to your web application's root directory, not inside the DLL. It has no way to "see" or serve files embedded in a DLL directly.
Workarounds to make this work
1. Deploy the ASPX page to your web application's directory
The simplest fix is to extract the Redirect.aspx file from the DLL and place it in a views folder at the root of your web application. Your existing redirect code will work immediately, since the web server can now find the page at the specified path.
2. Embed the ASPX as a resource and serve it via a proxy handler
If you need to keep the ASPX inside the DLL, mark it as an embedded resource (in Visual Studio, set the file's "Build Action" property to "Embedded Resource"). Then create a proxy in your web application to serve it:
- Add an ASHX handler (e.g.,
ResourceProxy.ashx) that reads the embedded ASPX content from the DLL. - Write the content to the response with the correct
text/htmlcontent type. - Redirect to this handler instead of the direct ASPX path.
Example handler code:
public class ResourceProxy : IHttpHandler { public void ProcessRequest(HttpContext context) { var dllAssembly = Assembly.Load("YourDllAssemblyName"); // Match the full embedded resource name (namespace + folder + filename) var resourcePath = "YourDllNamespace.views.Redirect.aspx"; using (var resourceStream = dllAssembly.GetManifestResourceStream(resourcePath)) { if (resourceStream != null) { context.Response.ContentType = "text/html"; using (var reader = new StreamReader(resourceStream)) { string pageContent = reader.ReadToEnd(); // Optional: Modify content to resolve relative paths for styles/scripts context.Response.Write(pageContent); } } else { context.Response.StatusCode = 404; context.Response.Write("Embedded page not found"); } } } public bool IsReusable => false; }
Update your redirect code to point to the proxy:
HttpContext.Current.Response.Redirect("/ResourceProxy.ashx", false);
3. Use Server.Transfer (with caveats)
If you're working with ASP.NET Web Forms, Server.Transfer lets you switch to another page without a client-side redirect—but this still requires the target page to be resolvable in the web application's context. You'll still need to either deploy the page or use the embedded resource proxy approach above, since Server.Transfer can't directly access DLL-embedded pages.
Quick notes on your existing code
- Always check if
HttpContext.Currentisnullbefore accessingSessionorResponse—if this method runs outside a web request (e.g., a background thread), you'll get a null reference exception. - Using
Response.Redirect(false)doesn't terminate the current thread immediately. Make sure no additional code runs after this call that could interfere with the redirect.
内容的提问来源于stack exchange,提问作者Chetan chadha

