简体   繁体   中英

How do I redirect someone from one action to another in ASP.NET MVC when an action has completed?

In my webapp (work in progress) I now try to allow new customers register to my webapp. I am deploying in Azure btw.

The idea I am working on:

www.[mysite].com/Register

This register page allows the "new" user to register his a new tenant.

customer.[mysite].com/Register

This pretty much looks as follows in code

    public ActionResult Register()
    {
        var url = Request.ServerVariables["HTTP_HOST"];
        var tenantNameParser = new TenantNameParser(url);

        if (!TenantRepository.Exists(tenantNameParser.TenantName))
        {
            return RedirectToAction("NewCustomer");
        }

        return View();
    }

In the above snippet I check if tenant exists, if not, redirect to new customer.

    [HttpPost]
    public ActionResult NewCustomer(Customer model)
    {
        if (ModelState.IsValid)
        {
            var tenant = new Tenant
                {
                    Name = model.TenantName,
                    Guid = Guid.NewGuid().ToString()
                };

            TenantRepository.Add(tenant);
            // TODO create a new user new tenant repository to do this stuff as 1 atomic action

            if (!UserRepository.Exists(model.AccountableEmailAddress))
            {
                // Attempt to register the user
                var createStatus = MembershipService.CreateUser(
                    model.AccountableUser,
                    model.Password,
                    model.AccountableEmailAddress);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.AccountableUser, false);
                    return RedirectToAction("Index", "Home");
                }

                throw new Exception("crappy implemenation, improve");
            }
            FormsService.SignIn(model.AccountableUser, false);

            // TODO figure out how to redirect to tenant.site.com
            return RedirectToAction("Index", "Home");
        }

        return View(model);
    }

The code above creates a new customer, and in the end my //todo is what this question is about. How to redirect to an URL that includes the actual tenant in the form of tenant.domain.com?

I don't know if this "way of handling tenants" is a bad practice, if so please feel free to say so.

Question: How to redirect to a tenant URL ([tenantname.[mysite].com/Index/Home). Something like: Redirect("tenantname", "Index", "Home") would be great, but of course doesn't exist. I google'd it for a bit, but didn't run into helpful links (that's mainly the reason why I think I am designing "the wrong thing").

Advice would be awesome! Thx a lot for your considerations in advance!

If I need to rephrase stuff because it's unclear what I ask for then please let me know.

为此,您需要查看ASP .NET MVC的域路由

Just my 2 cents, please correct me if it's wrong.

My understanding is, you cannot control the tenant name in [tenant name].yoursite.com in your code, as it's out of the scope of ASP.NET MVC. It's a CNAME of the domain yoursite.com. So some thoughts from my end below.

  1. I guess you need a program-able domain provider, which means you can create CNAME from your code. Then when a new customer registered, the actual URL might be yoursite.com/[tenant name]/. Then you map it to a new CNAME [tenant name].yoursite.com.

  2. I'm not sure an URL Rewrite can help you. Which means you create a new rule and if the incoming URL was [tenant name].yoursite.com, you map it to yoursite.com/[tenant name]/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM