题:
我正在创建一个wiki软件,基本上是维基百科/ mediawiki的克隆,但是在ASP.NET MVC中(MVC是重点,所以不建议我使用ScrewTurn).
现在我有一个问题:
我使用此路由映射来路由URL,如:http:
//en.wikipedia.org/wiki/ASP.NET
routes.MapRoute(
"Wiki", // Routenname
//"{controller}/{action}/{id}", // URL mit Parametern
"wiki/{id}", // URL mit Parametern
new { controller = "Wiki", action = "dbLookup", id = UrlParameter.Optional } // Parameterstandardwerte
);
Run Code Online (Sandbox Code Playgroud)
现在它刚刚发生在我身上,可能会出现像'AS/400'这样的标题:http:
//en.wikipedia.org/wiki/AS/400
在整体上,还有这个(标题'Slash'):http:
//en.wikipedia.org/wiki//
这一个:http:
//en.wikipedia.org/wiki//dev/null
总的来说,维基百科似乎有一个有趣的标题列表,如:http://en.wikipedia.org/wiki/Wikipedia : Articles_with_slashes_in_title
如何正确制作此路线的路线?
编辑:
类似于:
如果URL以/ Wiki /开头,如果它不以/ wiki/Edit /(但不是/ Wiki/Edit)开头,则将所有其余URL作为Id传递.
编辑:
嗯,只是另一个问题:我如何路由这个:http:
//en.wikipedia.org/wiki/C&A
维基百科可以......
编辑:
根据维基百科,由于与wikitext语法的冲突,在页面标题中永远不能使用以下字符(DISPLAYTITLE也不支持):
# < > [ ] | { …Run Code Online (Sandbox Code Playgroud) 我使用$ .ajax()每5秒轮询一次动作方法,如下所示:
$.ajax({
type: 'GET', url: '/MyController/IsReady/1',
dataType: 'json', success: function (xhr_data) {
if (xhr_data.active == 'pending') {
setTimeout(function () { ajaxRequest(); }, 5000);
}
}
});
Run Code Online (Sandbox Code Playgroud)
和ActionResult动作:
public ActionResult IsReady(int id)
{
if(true)
{
return RedirectToAction("AnotherAction");
}
return Json("pending");
}
Run Code Online (Sandbox Code Playgroud)
我必须将动作返回类型更改为ActionResult才能使用RedirectToAction(最初它是JsonResult并且我正在返回Json(new { active = 'active' };),但它看起来很难重定向并从$ .ajax()成功回调中呈现新视图.我需要从这个轮询ajax回发中重定向到"AnotherAction".Firebug的响应是来自"AnotherAction"的视图,但它不是渲染.
我正在用ASP.Net MVC 4重写一个网站项目,我发现很难设置正确的路线.url结构不是RESTful或遵循控制器/操作模式 - 页面具有以下slug结构.所有slugs都保存在数据库中.
/country
/country/carmake
/country/carmake/carmodel
/custom-landing-page-slug
/custom-landing-page-slug/subpage
Run Code Online (Sandbox Code Playgroud)
例:
/italy
/italy/ferrari
/italy/ferrari/360
/history-of-ferrari
/history-of-ferrari/enzo
Run Code Online (Sandbox Code Playgroud)
因为Country,Car Make并且Car Model是不同的模型/实体,我想有像CountriesController,CarMakesController和CarModelsController这样的东西,我可以处理不同的逻辑并从中呈现适当的视图.此外,我有自定义登录页面,可以有包含一个或多个斜杠的slugs.
我的第一次尝试是有一个全能的PagesController,它将在数据库中查找slug并根据页面类型调用适当的控制器(例如CarMakesController),然后执行一些逻辑并渲染视图.但是,我从未成功"调用"其他控制器并呈现适当的视图 - 而且感觉不对.
任何人都能指出我在正确的方向吗?谢谢!
编辑:为了澄清:我不想重定向-我要委派请求到不同的控制器,用于处理逻辑和呈现视图,取决于内容的类型(Country,CarMake等).
我正在尝试建立自己的小cms.我创建了一个抽象的pageBase类,它由Static,Reviews,Articles,News继承.每个都有自己的控制器方法.
我的问题是我需要允许管理员定义自己的自定义路径级别.例如news\local\mynewdog或Articles\events\conventions\mycon.所以我想要一种传递字符串数组并设置自定义路由的方法.
c# asp.net-mvc url-routing asp.net-mvc-routing custom-routes
我正在尝试将其用于登录页面.
if (Session["UserID"] == null)
Server.Transfer("/Account/Login", true);
Run Code Online (Sandbox Code Playgroud)
但是我得到了异常 - >执行子请求/帐户/登录时出错.
我正在使用ELMAH在ASP.NET Webforms应用程序中记录未处理的异常.记录工作正常.
我想将ELMAH错误日志ID传递给自定义错误页面,该页面将使用户能够通过电子邮件向管理员发送有关错误的信息.我听从了这个答案的建议.这是我的global.asax代码:
void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
Session[StateKeys.ElmahLogId] = args.Entry.Id;
// this doesn't work either:
// HttpContext.Current.Items[StateKeys.ElmahLogId] = args.Entry.Id;
}
Run Code Online (Sandbox Code Playgroud)
但是,在自定义错误页面上,会话变量引用和HttpContext.Current.Items给了我一个NullReference异常.如何将ID传递给我的自定义错误页面?
是否可以在不更改URL的情况下从一个控制器(A)操作重定向到另一个控制器(B)操作?
我尝试使用RedirectToAction但浏览器中的URL发生了变化
ASP.Net MVC控制器有几种将控制转发到另一个控制器或动作的方法.但是,所有这些都导致客户端重定向,具有302和新的浏览器请求.
为什么没有内部"调用另一个控制器的操作"功能,而不涉及客户端?我很确定php CodeIgniter框架也缺少这个功能.
我能提出的最好的理由是,最终它是不必要的,因为你想要调用的任何代码都可以被分解到某个常见的地方,至少在ASP.Net MVC中,操作可能非常昂贵.但是很多人都在问这个问题,看起来这最终会带来一些便利.
所以......很多聪明人设计了这些框架.没有这个必须有一些很好的理由吗?
任何想法如何解决以下问题?
有一个伟大的实施提供TransferResult的位置,这对MVC 1,2伟大的工作,但对MVC 3 RC不起作用.
public class TransferResult : RedirectResult
{
public TransferResult(string url): base(url)
{
}
public override void ExecuteResult(ControllerContext context)
{
var httpContext = HttpContext.Current;
httpContext.RewritePath(Url, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
}
}
Run Code Online (Sandbox Code Playgroud)
在MVC 3 RC上,httpHandler.ProcessRequest失败并说 'HttpContext.SetSessionStateBehavior' can only be invoked before 'HttpApplication.AcquireRequestState' event is raised.
如何重写此代码以使其工作?
UPD:如果在VS 2010内置开发服务器上运行,代码可以工作,但无法在IIS 7.5 localhost上运行.问题仍未解决.
知道了RedirectToAction,我正在寻找类似的东西,以保持用户的URL稳定,并仍然将责任从一个动作传递到另一个动作.
由于我在这个主题上找到了零搜索结果,我不妨完全尝试解决XY问题.
不过,我会试着解释为什么我认为可能需要这个.
场景:
public ActionResult A(int id)
{
var uniqueID = CalculateUniqueFromId(id);
// This compiles.
return RedirectToAction("B", new { uniqueID });
// This does not compile.
return RewriteToAction("B", new { uniqueID });
}
public ActionResult B(Guid uniqueID)
{
var model = DoCreateModelForUnique(uniqueID);
return View("B", model);
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,动作A从整数计算一个guid并将其传递给另一个动作.
解决方法:
我可以将上面的代码更改为:
public ActionResult A(int id)
{
var uniqueID = CalculateUniqueFromId(id);
var model = DoCreateModelForUnique(uniqueID);
return View("B", model);
}
public ActionResult B(Guid uniqueID)
{
var model …Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×9
c# ×4
.net ×3
asp.net ×3
ajax ×1
codeigniter ×1
elmah ×1
jquery ×1
url-routing ×1