You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何重定向至DLL文件中的Aspx页面?该操作是否可行?

Question: Can I redirect to an ASPX page stored inside a DLL from a method in the same DLL?

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?


Answer

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/html content 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.Current is null before accessing Session or Response—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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:52:36