Csharp/C#教程:如何在MVC中创建动态子域?分享


如何在MVC中创建动态子域?

我已将我的网站托管为www.sample.com,但我需要

可能吗? 每个用户作为子域名。

您实际上并不“创建”新的子域 – 而是设置“通配符”条目,作为所有未解析名称的全部内容:

  1. 您需要通配符DNS记录,以便所有子域解析为相同的主机(IP)地址。 并非所有DNS提供商都提供此服务,因此您可能需要在必要时切换DNS服务。
  2. 然后,您需要配置Web服务器以处理这些请求。 你有两个选择:
    1. 使用端点绑定(因此您的IIS网站将处理对给定IP地址和端口号的所有请求)。 在IIS 10.0之前,这是支持通配符或任意子域的唯一方法。 此方法需要为每个网站单独的IP地址。
    2. 使用正确的通配符域绑定。 (IIS最终在10.0版中添加了对此function的支持)[0](IIS 10.0随Windows 10和Windows Server 2016发布)。 IIS 8.5或更早版本不支持此function。
  3. 您的应用程序代码需要检查HTTP Host:标头以确定检查的内容。 如果您使用RouteBase ASP.NET(ASP.NET MVC自动执行),您可以使用自定义RouteBase子类来让您在路由中使用Host头值,如下例所示: http : //benjii.me/2015 / 02 /子域路由function于ASP净MVC /

请注意,步骤2的两个选项都需要客户服务器配置选项(IP地址绑定或运行Windows Server 2016)。 如果您正在使用共享托管服务提供商(HostGator,DreamHost等 – 甚至是Azure网站),那么您将无法使用此function,因为它们通常不向客户提供这些function – 但无论如何都要求他们查看是否可以支持你。

上述就是C#学习教程:如何在MVC中创建动态子域?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 public class SubdomainRoute : RouteBase { public override RouteData GetRouteData(HttpContextBase httpContext) { var host = httpContext.Request.Url.Host; var index = host.IndexOf("."); string[] segments = httpContext.Request.Url.PathAndQuery.Split('/'); if (index < 0) return null; var subDomain = host.Substring(0, index); string controller = segments[1]; if (string.IsNullOrEmpty(controller)) { controller = "Home"; } string action = segments.Count() > 2 ? segments[2] : string.Empty; if (string.IsNullOrEmpty(action)) { action = "Index"; } var routeData = new RouteData(this, new MvcRouteHandler()); routeData.Values.Add("controller", controller); //Goes to the relevant Controller class routeData.Values.Add("action", action); //Goes to the relevant action method on the specified Controller routeData.Values.Add("subdomain", subDomain); //pass subdomain as argument to action method return routeData; } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { //Implement your formating Url formating here return null; } } public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.Add(new SubdomainRoute()); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 

 **In Visual Studio 2015,2017 we need to change how IIS Express runs your development environment. In Visual Studio 2015,2017 right-click on the solution file. Select 'Open Folder in File Explorer' Go to the containing folder and look for a .vs folder. Inside, there will be a config folder containing an applicationhost.config file. Open it in your favorite text editor (I use Notepad++) Under the Configuration/system.applicationHost/Sites, you should see your site in bindings with a port and localhost.  Duplicate this line and add your subdomain to this binding.   Save this file and reload your project. That was to configure your site in Visual Studio. Now we need to head to our Hosts file to modify that as well. Go to C:WindowsSystem32DriversEtc in File Explorer. Open the HOSTS file (it has no extension) At the bottom of the hosts file, add the following two lines. # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost #127.0.0.1 shopservice.com 127.0.0.1 ans.shopservice.com #127.0.0.1 waqar.shopservice.com Save the file. Now we can run our Visual Studio and see our subdomain actually run.** 

 public class HomeController : Controller { DomainUsersEntities dbContext = new DomainUsersEntities(); public ActionResult Index(string subdomain) { Session["SubDomain"] = subdomain; //var result = dbContext.Companies.Where(x => x.CompanyName == subdomain).FirstOrDefault(); return View(); } [HttpPost] public ActionResult LoginIndex(LoginModel loginModel) { var subdomaincheck = Session["SubDomain"]; if (subdomaincheck != null) { var result = dbContext.Companies.Where(x => x.CompanyName == subdomaincheck).FirstOrDefault(); if (result != null) { var userchek = dbContext.Users.Where(x => x.Name == loginModel.UserName && x.Company.CompanyName==subdomaincheck).FirstOrDefault(); if (userchek != null) { TempData["domainuser"] = "Weclome on " + subdomaincheck + " subdomain"; return RedirectToAction("About", "Home"); } else { TempData["InValidUSER"] = "You Asscess wrong subdomain!"; return RedirectToAction("Index", "Home"); } } else { TempData["InValidUSER"] = "No"+ subdomaincheck + "domain found "; return RedirectToAction("Index", "Home"); } } else { TempData["InValidUSER"] = "Invalid Username/Password!"; return RedirectToAction("Adminlogin", "Account"); } //var result = dbContext.Companies.Where(x => x.CompanyName == subdomain).FirstOrDefault(); }} 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1011539.html

(0)
上一篇 2021年12月30日
下一篇 2021年12月30日

精彩推荐