[英]MVC4 Dynamic Controller Path?
I have a SiteController class in my MVC4 project,the 'Site' url is working fine, but I need a dynamic url part right after 'Site', I need the url to look like this: 我在我的MVC4项目中有一个SiteController类,'Site'网址工作正常,但我需要在'Site'之后的动态网址部分,我需要网址看起来像这样:
mysite.com/Site/{DYNAMICSTRING}/Users(or whatever) mysite.com/Site/{DYNAMICSTRING}/Users(或其他)
{DYNAMICSTRING} can be the name of a subsite, so the controller should check if that subsite does actually exist in the database. {DYNAMICSTRING}可以是子网站的名称,因此控制器应检查该子网站是否确实存在于数据库中。
Right now I'm using Query strings, but that is not what my client wants. 现在我正在使用查询字符串,但这不是我的客户想要的。
How can I do that? 我怎样才能做到这一点?
Additional details 额外细节
My routing: 我的路由:
routes.MapRoute(
"Subdomain", // Route name
"{controller}/{action}/{dynamicString}", // URL with parameters
new { controller = "Site", action = "Subdomain" } // Parameter defaults
);
My controller: 我的控制器:
public ActionResult Subdomain(string dynamicString)
{
return View();
}
the value of dynamicString is null when I navigate to: /Site/Subdomain/SomeString 当我导航到:/ Site / Subdomain / SomeString时,dynamicString的值为null
You have to configure routing. 您必须配置路由。 For example if you have Homecontroller: 例如,如果你有Homecontroller:
public class HomeController:Controller
{
public ActionResult Subdomain(string dynamicString)
{
return View();
}
}
then you have to configure your routing something like that 那么你必须配置类似的路由
routes.MapRoute(
"Subdomain", // Route name
"{controller}/{action}/{dynamicString}/anyOtherParams", // URL with parameters
new { controller = "Home", action = "Subdomain", dynamicString = "" } // Parameter defaults
);
You can do it like this: 你可以这样做:
routes.MapRoute(
name: "Default", // Route name
url:"Site/{dynamicstring}", // URL with parameters
defaults: new {controller = "Site", action = "Index" } // Defaults
);
you can keep adding parts to the url part like so 你可以像这样继续在url部分添加部分
url:"Site/{dynamicstring}/{anythingelse}" // url:"Site/{dynamicstring}/{anythingelse}/{bla}/Test/{hello}/etc..."
or you can also have a catch all route like this:" 或者你也可以像这样抓住所有路线:“
routes.MapRoute(
name: "Default", // Route name
url:"{*all}", // catch all
defaults: new {controller = "Site", action = "Index" } // Defaults
);
and fetch all other parts in your controllers index action, by splitting them on / 并在控制器索引操作中获取所有其他部分,方法是将它们拆分为/
Make sure you put the custom route before your default otherwise the default route will pick it up. 确保将自定义路由放在默认路由之前,否则默认路由将将其拾取。
in your controller you get something like this: 在你的控制器中,你得到这样的东西:
public ActionResult Index(string dynamicstring, string anythingelse)
{
return View();
}
and if you then pass in a url like this: 如果你然后传入这样的网址:
http://www.mysite.com/Site/test.nl/jordy http://www.mysite.com/Site/test.nl/jordy
your dynamicstring will have the value "test.nl" and your anythingelse will have "jordy" I hope this helps 你的dynamicstring将具有值“test.nl”,你的anythingelse将有“jordy”我希望这有助于
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.